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

huangapple go评论72阅读模式
英文:

Who selects which overloaded function to be executed in Java?

问题

谢谢你的理解。以下是你要翻译的内容:

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

我的问题是,是谁选择了这个函数?是Java编译器还是JVM?

英文:

I'm learning Java and I have a question.
We can overload a method in various ways and the program executes just one. So the program has to determine which function to use.

My question is, who selects the function? Is it java compiler, or JVM?

答案1

得分: 4

方法重载是由Java编译器在编译时根据参数处理的。以下是方法重载的一个示例:

class Main{  
  void func(){System.out.println("无参数");}  
  void func(int a){System.out.println("有参数");}   
}

你可以在这个链接中详细了解更多信息:https://www.infoworld.com/article/3268983/java-challengers-1-method-overloading-in-the-jvm.html

英文:

Method overloading is handled by the java compiler at compile time based on arguments. Here's an example for method overloading:

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

Here's a link where you can learn more about it in detail: https://www.infoworld.com/article/3268983/java-challengers-1-method-overloading-in-the-jvm.html

答案2

得分: 4

方法重载的另一个名称是**“编译时多态性”**。在编译时,Java通过检查方法签名来确定调用哪个方法。因此,这被称为编译时多态性或静态绑定。

有两种类型的多态性。另一种名为**“运行时多态性”**的多态性。这是通过方法重写(类中的父/子关系)来实现的。对象在运行时与功能绑定。Java虚拟机在运行时确定要调用的适当方法,而不是在编译时确定。

英文:

Another name to Method Overload is "compile-time polymorphism". At compile-time, java knows which method to call by checking the method signatures. So this is called compile-time polymorphism or static or early binding.

There are two types of polymorphisms. Another kind of polymorphism named as "run-time polymorphism". This is achieved by Method Overriding (parent/child relationship in classes). Object is bound with the functionality at run time. Java virtual machine determines the proper method to call at the runtime, not at the compile time.

huangapple
  • 本文由 发表于 2020年10月8日 20:37:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64262694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定