谁来选择Java中要执行的重载函数?

Who selects which overloaded function to be executed in Java?

我正在学习 Java,我有一个问题。 我们可以通过多种方式重载一个方法,而程序只执行其中一种。所以程序必须确定使用哪个函数。

我的问题是,谁来选择这个功能?是 java 编译器,还是 JVM?

方法重载由 java 编译器在编译时根据参数处理。这是方法重载的示例:

class Main{  
  void func(){System.out.println("No Params");}  
  void func(int a){System.out.println("Has Params");}   
}

这里有一个link,您可以在其中详细了解它:https://www.infoworld.com/article/3268983/java-challengers-1-method-overloading-in-the-jvm.html

方法重载的另一个名称是 "compile-time 多态性"。在 compile-time,java 通过检查方法签名知道调用哪个方法。所以这叫做 compile-time 多态性或静态或早期绑定。

There are two types of polymorphisms。另一种多态性命名为run-time多态性。这是通过方法覆盖实现的(parent/child 关系在 类 中)。对象在 运行 时间与功能绑定。 Java 虚拟机在 运行 时而不是在编译时确定要调用的正确方法。