我一直在学习,因为我参加了考试,而且我对大多数Java没有太多问题,但我偶然发现了一个我无法解释的规则.这是一段代码片段:
public class A {
public int method(Object o) {
return 1;
}
public int method(A a) {
return 2;
}
}
public class AX extends A {
public int method(A a) {
return 3;
}
public int method(AX ax) {
return 4;
}
}
public static void main(String[] args) {
Object o = new A();
A a1 = new A();
A a2 = new AX();
AX ax = new AX();
System.out.println(a1.method(o));
System.out.println(a2.method(a1));
System.out.println(a2.method(o));
System.out.println(a2.method(ax));
}
Run Code Online (Sandbox Code Playgroud)
返回:
1 3 1 …