Java重载规则

标签 java overloading

我最近遇到了两个重载问题,我找不到答案并且没有 java 环境来运行一些测试代码。我希望有人可以通过汇编 Java 编译器遵循的所有规则的列表来帮助我进行重载,或者交替地将我指向一个已经存在的列表。

首先,当两个方法仅在最终可变参数上有所不同时,在什么情况下会调用每个方法,您可以在没有任何参数的情况下调用可变参数方法吗?

private void f(int a) { /* ... */ }
private void f(int a, int... b) { /* ... */ }

f(12); // calls the former? I would expect it to
f(12, (int[])null); // calls latter, but passes null for b? 
  // Can I force the compiler to call the second method in the same fashion
  // as would happen if the first method didn't exist?

第二个问题,当两种方法因从彼此继承的类型不同而被调用时?我希望调用最派生的版本,并允许调用另一个版本。

interface A {}
class B implements A {}
class C implements A {}

private void f(A a) {}
private void f(B b) {}

f(new C()); // calls the first method
f(new B()); // calls the second method?
f((A)(new B()); // calls the first method using a B object?

这是两个例子,但作为代码阅读者,我更喜欢用于解决此问题的准确有序规则的规范列表,因为我经常没有时间设置构建环境来检查编译器是什么做。

最佳答案

重载与覆盖

方法的正确实现的选择是在运行时完成的,正如您所指出的,现在要调用的方法的签名是在编译时决定的。

编译时重载方法选择

Java Language Specification (JLS) 第 15.12 节 Method Invocation Expressions详细解释了编译器选择正确调用方法所遵循的过程。

在那里,您会注意到这是一个编译时任务。 JLS 在第 15.12.2 小节中说:

This step uses the name of the method and the types of the argument expressions to locate methods that are both accessible and applicable There may be more than one such method, in which case the most specific one is chosen.

通常情况下,可变参数方法是最后选择的,如果它们与其他候选方法竞争,因为它们被认为不如接收相同参数类型的方法具体。

要验证其编译时性质,您可以进行以下测试。

像这样声明一个类并编译它(即 javac ChooseMethod.java)。

public class ChooseMethod {
   public void doSomething(Number n){
    System.out.println("Number");
   }
}

声明第二个类调用第一个类的方法并编译它(即 javac MethodChooser.java)。

public class MethodChooser {
   public static void main(String[] args) {
    ChooseMethod m = new ChooseMethod();
    m.doSomething(10);
   }
}

如果您运行该程序(即 java MethodChooser),输出为 Number

现在,向 ChooseMethod 类添加第二个更具体的重载方法,并重新编译它(但不要重新编译其他类)。

public void doSomething(Integer i) {
 System.out.println("Integer");
}

如果再次运行 main,输出仍然是 Number

基本上,因为它是在编译时决定的。如果您重新编译 MethodChooser 类(带有 main 的那个),并再次运行该程序,输出将是 Integer

因此,如果您想强制选择其中一个重载方法,则参数的类型必须与编译时的参数类型相对应,而不仅仅是在运行时。

在运行时覆盖方法选择

同样,方法的签名是在编译时决定的,但实际的实现是在运行时决定的。

像这样声明一个类并编译它。

public class ChooseMethodA {
   public void doSomething(Number n){
    System.out.println("Number A");
   }
}

然后声明第二个扩展类并编译:

public class ChooseMethodB extends ChooseMethodA {  }

在 MethodChooser 类中,您可以:

public class MethodChooser {
    public static void main(String[] args) {
        ChooseMethodA m = new ChooseMethodB();
        m.doSomething(10);
    }
}

如果您运行它,您会得到输出 Number A,这没问题,因为该方法尚未在 ChooseMethodB 中被覆盖,因此调用的实现是ChooseMethodA 的那个。

现在,在 MethodChooserB 中添加一个覆盖方法:

public void doSomething(Number n){
    System.out.println("Number B");
}

然后重新编译这个,然后再次运行 main 方法。

现在,您得到输出 Number B

因此,实现是在运行时选择的,不需要重新编译 MethodChooser 类。

关于Java重载规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10901259/

相关文章:

C++ 重载函数和错误

java - ExtJs 不会将数据加载到表单

java - CXF,wsdl2java。自定义端口名称

c++ - 调用另一个函数重载

scala - Scala 案例类的重载构造函数?

java - Retrofit 2 调用覆盖

从 url 解析 xml 文件时发生 java 错误

java - 使用 JScrollPane 缩小 JPanel

java - 在状态模式中实现继承层次结构

c++ - 是否可以在 C++ 中进行多态成员重载?