Java 检查有效的重载
我想知道这是否是有效的重载:Java 检查有效的重载,java,oop,overloading,Java,Oop,Overloading,我想知道这是否是有效的重载: public class OverLoadingTest{ private void callFunction(Object object){ System.out.println("Printing Object"); } private void callFunction(String string){ System.out.println("Printing String
public class OverLoadingTest{
private void callFunction(Object object){
System.out.println("Printing Object");
}
private void callFunction(String string){
System.out.println("Printing String");
}
}
还有,因为有人问我这个问题。
如果我真的喜欢这个
OverLoadingTest test = new OverLoadingTest();
test.callFunction(null);
将打印什么
当然,我的观点是,它根本不是有效的重载。
所以第二部分没有问题
请告诉我一些解释。调用具有最少泛型参数的方法。因此,在您的情况下,它将是方法接受String
注意:如果两个类处于同一级别,那么您将得到一个不明确的调用异常。例如,如果一个方法采用String
,另一个方法采用Exception
,则调用具有最少泛型参数的方法。因此,在您的情况下,它将是方法接受String
If more than one member method is both accessible and applicable to a method
invocation, it is necessary to choose one to provide the descriptor for
the run-time method dispatch.
The Java programming language uses the rule that the most specific method is chosen.
注意:如果两个类处于同一级别,那么您将得到一个不明确的调用异常。例如,如果一个方法采用字符串
,另一个方法采用异常
If more than one member method is both accessible and applicable to a method
invocation, it is necessary to choose one to provide the descriptor for
the run-time method dispatch.
The Java programming language uses the rule that the most specific method is chosen.
请参阅中的更多详细信息
- 在您的情况下,如果参数为
或String
,则将调用String方法,对于其他参数的类型,将调用null
方法对象
- 在您的示例中,如果您定义了另一个参数类型不是
(例如Integer)的方法,则无法编译源代码,因为在String
和String
的方法之间调用是不明确的,因为它们是相同级别的Integer
- 在您的情况下,如果参数为
或String
,则将调用String方法,对于其他参数的类型,将调用null
方法对象
- 在您的示例中,如果您定义了另一个参数类型不是
(例如Integer)的方法,则无法编译源代码,因为在String
和String
的方法之间调用是不明确的,因为它们是相同级别的Integer
IOException
和Exception
,将打印IOException
,因为它是Exception
的子类(最不通用)。同样的概念也适用于字符串
和对象
。因为String
是对象的子类。现在,如果您有IOException
和interruptedeexception
,那么调用将是不明确的,因为这两个类处于同一级别。明白了吗?而且,类之间必须有关系FileNotFoundException
和String
将给出编译时错误。请解释最后一行如果一个方法采用String,另一个方法采用Exception
@Shail016-请参见,如果类层次结构中有两个类处于不同级别,则将选择较低级别的类。因此,如果参数是IOException
和Exception
,将打印IOException
,因为它是Exception
的子类(最不通用)。同样的概念也适用于字符串
和对象
。因为String
是对象的子类。现在,如果您有IOException
和interruptedeexception
,那么调用将是不明确的,因为这两个类处于同一级别。明白了吗?而且,类之间必须有关系FileNotFoundException
和String
将给出编译时错误