What if you give the method the same names?

wetHands04 :

In the code below, two methods are named by the same name, what are the consequences?

And another question, how can I reduce the amount of code?

public String getFileName(Uri uri) {
    String fileName = null;
    String scheme = uri.getScheme();

    if (scheme == null)
        return null;

    if (scheme.equals("file")) {
        return uri.getLastPathSegment();
    } else if (scheme.equals("content")) {
        Cursor cursor = mContext.getContentResolver().query(uri, null, null, null, null);

        if (cursor != null) {
            if (cursor.getCount() != 0) {
                int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DISPLAY_NAME);
                cursor.moveToFirst();
                fileName = cursor.getString(columnIndex);
            }
            cursor.close();
        }
    }

    return fileName;
}

public static String getFileName(String path) {
    if (path == null)
        return null;

    int index = path.lastIndexOf(PATH_SEPERATOR);
    return index < path.length() ? path.substring(index + 1) : null;
}
Vitaly Kalenik :

You are talking about methods, right?
So you are using method overloading here. There are no consequences, if only you do not get confused with a large number of overloaded methods.

Method Overloading is a feature that allows a class to have more than one method having the same name, if their argument lists are different. It is similar to constructor overloading, that allows a class to have more than one constructor having different argument lists.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=183021&siteId=1