Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检查有效的重载_Java_Oop_Overloading - Fatal编程技术网

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
    null
    ,则将调用String方法,对于其他参数的类型,将调用
    对象
    方法
  • 在您的示例中,如果您定义了另一个参数类型不是
    String
    (例如Integer)的方法,则无法编译源代码,因为在
    String
    Integer
    的方法之间调用是不明确的,因为它们是相同级别的
请参阅中的更多详细信息

  • 在您的情况下,如果参数为
    String
    null
    ,则将调用String方法,对于其他参数的类型,将调用
    对象
    方法
  • 在您的示例中,如果您定义了另一个参数类型不是
    String
    (例如Integer)的方法,则无法编译源代码,因为在
    String
    Integer
    的方法之间调用是不明确的,因为它们是相同级别的

@FastSnail-不。它将用字符串参数调用方法是的,我错了,我忘了possibility@FastSnail-没有。它将使用字符串参数调用方法是的,我错了。我忘记了最小可能性。请解释最后一行<代码>如果一个方法使用字符串,另一个方法使用异常@Shail016-请看,如果两个类在类层次结构中处于不同级别,那么将选择较低级别的类。因此,如果参数是
IOException
Exception
,将打印
IOException
,因为它是
Exception
的子类(最不通用)。同样的概念也适用于
字符串
对象
。因为
String
对象的子类。现在,如果您有
IOException
interruptedeexception
,那么调用将是不明确的,因为这两个类处于同一级别。明白了吗?而且,类之间必须有关系
FileNotFoundException
String
将给出编译时错误。请解释最后一行
如果一个方法采用String,另一个方法采用Exception
@Shail016-请参见,如果类层次结构中有两个类处于不同级别,则将选择较低级别的类。因此,如果参数是
IOException
Exception
,将打印
IOException
,因为它是
Exception
的子类(最不通用)。同样的概念也适用于
字符串
对象
。因为
String
对象的子类。现在,如果您有
IOException
interruptedeexception
,那么调用将是不明确的,因为这两个类处于同一级别。明白了吗?而且,类之间必须有关系
FileNotFoundException
String
将给出编译时错误