Why is one class valid while the other is not?

情到浓时终转凉″ 提交于 2019-12-12 13:58:52

问题


As you can see, having a non void return type is important.

class TestValid {

public String f(List<String> list) {
    return null;
}

public Integer f(List<Integer> list) {
    return null;
}

public void test() {
    f(Arrays.asList("asdf"));
    f(Arrays.asList(123));
}

}

class TestInvalid {

public void f(List<String> list) {
    System.out.println("strings");
}

public void f(List<Integer> list) {
    System.out.println("numbers");
}

}

回答1:


TestValid isn't valid to start with:

TestValid.java:9: name clash: f(List<Integer>) and f(List<String>)
have the same erasure
public Integer f(List<Integer> list) {
               ^

So the return type is a red herring. If return types were taken into account in determining signatures for overloading, then it would be valid, but currently it's as invalid as this:

// Equally invalid
public Integer f() {}
public String f() {}

If you genuinely managed to get TestValid to compile, I'd be interested to know which compiler you're using.




回答2:


In the second case, the methods f cannot be distinguished at runtime due to type-erasure.

http://java.sun.com/docs/books/tutorial/java/generics/erasure.html

Thus they both have precisely the same signature.




回答3:


After the type erasure part of compilation, List<String> and List<Integer> are both really type List; in TestInvalid you are making two methods with the same runtime signature.




回答4:


Covariant return type prior to JDK1.5 was not supported; think of Object.clone() method. The following might be of interest:

public class Base {
    public String f_array(List<String> strings) {
        StackTraceElement current = Thread.currentThread().getStackTrace()[1];
        System.out.println(String.format("%s#%s(strings)", current.getClassName(), current.getMethodName()));
        return null;
    }

    public Integer f_array(List<Integer> ints) {
        StackTraceElement current = Thread.currentThread().getStackTrace()[1]; 
        System.out.println(String.format("%s#%s(ints)", current.getClassName(), current.getMethodName()));
        return null;
    }

    public Number f() {
        StackTraceElement current = Thread.currentThread().getStackTrace()[1]; 
        System.out.println(String.format("%s#%s()", current.getClassName(), current.getMethodName()));
        return null;
    };

    public static class Child extends Base {
        @Override
        public Integer f() { //note Integer is_a Number
            StackTraceElement current = Thread.currentThread().getStackTrace()[1]; 
            System.out.println(String.format("%s#%s()", current.getClassName(), current.getMethodName()));
            return null;
        }
    }

    public static void main(String... args) {
        Base c = new Base();
        c.f_array(Arrays.asList(1));
        c.f_array(Arrays.asList("1"));
        c.f();
        c = new Child();
        c.f_array(Arrays.asList(1));
        c.f_array(Arrays.asList("1"));
        c.f();
    }
}



回答5:


For TestValid class: the functions appear to be overloaded. You wont get a compile time error if the caller passes a parameter List object which is type defined.eg: new ArrayList<Integer>() or new ArrayList<String>(). as the signature (return type and input parameters) after type erasure is not same. However you will get compile time error if the you pass in new ArrayList().

The second definition violates the basic principle of overloading a function where two functions with same signature (return type and input parameters) after type erasure.



来源:https://stackoverflow.com/questions/3105177/why-is-one-class-valid-while-the-other-is-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!