没见过你这么玩的,呵呵下面这段程序也许能更好地说明问题,其中参数分别代表父类和他的子类 public void method(Object o) {
System.out.println("Object Version");
}
public void method( Child d) {
System.out.println("Child Version");
}
public void method(Parent i) {
System.out.println("Parent Version");
}
public static void main(String[] args) {
Test test = new Test();
Object o=new Object();
test.method(null);
}java函数调用的时候会自动从子类开始检测
如果程序如下,怎会出错
public class Test {
public void method(Object o) {
System.out.println("Object Version");
}
public void method(String s) {
System.out.println("String Version");
}
public void method(Integer i){
System.out.println("Integer Version"); }
public static void main(String[] args) {
Test test = new Test();
Object o=new Object();
test.method(null);
}
}
System.out.println("Object Version");
}
public void method( Child d) {
System.out.println("Child Version");
}
public void method(Parent i) {
System.out.println("Parent Version");
}
public static void main(String[] args) {
Test test = new Test();
Object o=new Object();
test.method(null);
}java函数调用的时候会自动从子类开始检测
如果程序如下,怎会出错
public class Test {
public void method(Object o) {
System.out.println("Object Version");
}
public void method(String s) {
System.out.println("String Version");
}
public void method(Integer i){
System.out.println("Integer Version"); }
public static void main(String[] args) {
Test test = new Test();
Object o=new Object();
test.method(null);
}
}
我想再问一下,为什么我的程序(就是问题里的)运行后世String Version 而不是Object Version.
还有什么叫“java函数调用的时候会自动从子类开始检测“
谢谢
System.out.println("String Version");
}
public void method(Integer i){ System.out.println("Integer Version");
}如果这两个函数同时存在于test类时,产生如下问题:
reference to method is ambiguous; both method method(java.lang.String) and method method(java.lang.Integer) 也就是说null 在当做参数传递时,肯定不做为对象来处理,但又不能明确系统具体做为什么类型或者什么值来处理。