Java 中方法的签名是否包括其返回类型? [英] Does a method's signature in Java include its return type?
问题描述
Java 类/接口中的方法签名是否包含其返回类型?
Does the method signature in a Java class/interface include its return type?
示例:
Java 知道这两种方法的区别吗:
Does Java know the difference between those two methods:
public class Foo {
public int myMethod(int param) {}
public char myMethod(int param) {}
}
或者只是方法名称和参数列表很重要?
Or is it maybe only the method name and parameters list that matter?
推荐答案
引用自 Oracle 文档:
定义:方法声明的两个组成部分包括方法签名——方法的名称和参数类型.
Definition: Two of the components of a method declaration comprise the method signature—the method's name and the parameter types.
由于编辑了问题以包含此示例:
Since the question was edited to include this example:
public class Foo {
public int myMethod(int param) {}
public char myMethod(int param) {}
}
不,编译器不会知道区别,因为它们的签名:myMethod(int param)
是相同的.第二行:
No, the compiler won't know the difference, as their signature: myMethod(int param)
is the same. The second line:
public char myMethod(int param) {}
会给你错误:方法已经在类中定义,进一步证实了上面的说法.
will give you can error: method is already defined in class, which further confirms the above statement.
这篇关于Java 中方法的签名是否包括其返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!