将null传递给重载方法 [英] Passing null to the Overloaded methods
问题描述
我对以下两个程序的输出感到困惑。
I am confused with the Output of the following two programs.
当我只有两个带参数的方法 String
和对象
在程序1中,它输出为 String
。
When I'm only having two methods with parameters as String
and Object
in Program 1 it gives me output as String
.
但是当我在程序2中添加一个参数为 Integer
的新方法时,它将无法编译并出现错误,因为
nullTest(Object)方法对于类型testNull不明确
But when I add a new method with parameter as Integer
in Program 2 it won't compile and gives error as
The method nullTest(Object) is ambiguous for the type testNull
程序1:
Program 1 :
package onkartest;
public class TestNull {
public static void nullTest(Object b)
{
System.out.println("object");
}
public static void nullTest(String x)
{
System.out.println("String");
}
public static void main(String x[])
{
nullTest(null);
}
}
输出:String
计划2:
Program 2 :
package onkartest;
public class TestNull {
public static void nullTest(Object b)
{
System.out.println("object");
}
public static void nullTest(String x)
{
System.out.println("String");
}
public static void nullTest(Integer i)
{
System.out.println("Integer ");
}
public static void main(String x[])
{
nullTest(null);
}
}
输出:
线程main中的异常java.lang.Error:未解决的编译问题:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
对于类型testNull
The method nullTest(Object) is ambiguous for the type testNull
onkartest.testNull.main(testNull.java:26)
at onkartest.testNull.main(testNull.java:26)
如果我运行程序只保留 Object
参数方法,它会输出对象
。
And also if I run the program with keeping only Object
parameter method, it gives me output as Object
.
你能解释一下这种行为背后的原因吗?
Can you explain me the reason behind this behavior?
推荐答案
原因是在方法重载的情况下,当传递一个可以被子类和父类引用的参数时,具有子类类型参数的方法将总是被调用。
The reason is that in case of method overloading,when a parameter is passed that can be referenced by both child and parent class, the method with the parameter of type child class will always be called.
String是Object的子类,因此调用它的方法。
The String is a subclass of Object and therefore its method is called.
在下面的例子中,我们有类 Parent
可被视为对象
类和子
类可被视为 String
class。
In the following example we have class Parent
that can be considered as Object
class and Child
class that can be considered as String
class.
现在你的程序1会发生什么,完全相同的事情,就是带有子类对象参数的方法被调用
Now what happens with your program 1, exactly the same thing happens, that is the method with parameter of child class object gets called
public class Program
{
public static void method(Parent p1)//Consider the Parent as Object
{
System.out.println("parent");
}
public static void method(Child c)//Consider the Child as String
{
System.out.println("Child");
}
public static void main(String []args)
{
method(null);
}
}
class Parent//consider Parent as Object
{
}
class Child extends Parent//consider Child as String
{
}
但是现在假设您添加以下内容class Child2
可以视为 Integer
类。 Child2
是 Parent
的子类,就像 Integer
一样 Object的子类
But suppose now you add the following class Child2
that can be considered as your Integer
class. Child2
is a subclass of Parent
, just like Integer
is a subclass of Object
class Child2 extends Parent
{
}
并添加以下方法
public static void method(Child2 c)//Consider the Child2 as Integer
{
System.out.println("Child");
}
现在发生的事情正是程序中发生的事情2.原因是编译器无法决定哪一个是正确的,因为它们都是同一个父类的子类,而最重要的是它们都没有(Child2或Child)是另一个的父类。
Now what happens is exactly what happend in your program 2. The reason is that the compiler is not able to decide which is the right one because of the ambiguity because both are child class of the same same parent class and most importantly none of them(Either Child2 or Child) is a parent of the other.
如果我们有一个案例,我们将2个类中的任何一个作为另一个类的子类并且都继承了父类,那么没有歧义,将调用带有子类的方法。
If we had a case where we have either of the 2 classes a subclass of the other and both inherit Parent class, then there would be no ambiguity and the method with child class will be called.
这篇关于将null传递给重载方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!