两个 Java 方法可以具有相同的名称但返回类型不同吗?
两个 Java 方法可以具有相同名称但返回类型不同吗?方法的返回类型不同,并且使用相同的方法名称进行声明。
这是允许的吗?
Can two Java methods have the same name with different return type? The return type of the methods are different and they are declared with the same method's name.
Is that allowed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果两个方法具有相同的参数类型,但返回类型不同,则这是不可能的。来自 Java 语言规范,Java SE 8 版本,§8.4 .2.方法签名:
如果两种方法具有不同的参数类型(因此,它们具有不同的签名),那么这是可能的。这称为超载。
If both methods have same parameter types, but different return type than it is not possible. From Java Language Specification, Java SE 8 Edition, §8.4.2. Method Signature:
If both methods has different parameter types (so, they have different signature), then it is possible. It is called overloading.
仅当它们接受不同的参数时。如果没有参数,那么必须有不同的名称。
Only, if they accept different parameters. If there are no parameters, then you must have different names.
然而,根据 JLS,由于 Java 6/7 编译器(Oracle 的 JDK、OpenJDK、IBM 的 JDK)中的功能/错误,如果您使用泛型,您不能为相同的方法签名拥有不同的返回类型。
如需了解更多详细信息,请
在此处阅读我的文章
According the JLS, you cannot however due to a feature/bug in the Java 6/7 compiler (Oracle's JDK, OpenJDK, IBM's JDK) you can have different return types for the same method signature if you use generics.
Prints
For more details, read my article here
不可以。C++ 和 Java 都不允许重载函数的返回类型。原因是返回类型的重载可能会令人困惑(开发人员很难预测将调用哪个重载)。事实上,有人认为任何重载都可能在这方面造成混乱,并建议反对它,但即使是那些赞成重载的人似乎也同意这种特殊形式太令人困惑了。
No. C++ and Java both disallow overloading on a functions's return type. The reason is that overloading on return-type can be confusing (it can be hard for developers to predict which overload will be called). In fact, there are those who argue that any overloading can be confusing in this respect and recommend against it, but even those who favor overloading seem to agree that this particular form is too confusing.
仅当其中一个方法是继承的并且返回类型兼容时,才可以使用两个具有相同参数和不同返回类型的方法。
例如:
You can have two methods with the same arguments and different return types only if one of the methods is inherited and the return types are compatible.
For example:
java 文档指出:
请参阅: http://docs.oracle.com/javase/tutorial/java /javaOO/methods.html
The java documentation states:
See: http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
即使这是一个旧线程,也许有些人感兴趣。
如果您可以选择在同一个类中使用相同方法并归档不同的返回类型,请使用泛型:Oracle 课程泛型
泛型值持有者类的简单示例:
并像这样使用它:
... 将产生以下输出:
Even if it is an old thread, maybe some is interested.
If it is an option to you to use the same method inside the same class and archive different return types, use generics: Oracle Lesson Generics
Simple example for generic value holder class:
And use it like this:
... will result in the following output:
仅当它们的参数声明与内存不同时。
Only if their parameter declarations are different from memory.
如果它在同一个类中,具有相同数量的参数、相同的类型和顺序,
那么这是不可能的,例如:
如果参数的数量及其类型相同但顺序不同,那么这是可能的,因为它会导致方法重载。这意味着方法签名是否相同,其中包括方法名称、参数数量及其类型以及定义的顺序。
If it's in the same class with the equal number of parameters with the same types and order,
then it is not possible for example:
if the number of parameters and their types is same but order is different then it is possible since it results in method overloading. It means if the method signature is same which includes method name with number of parameters and their types and the order they are defined.