java 所以在java中你不能有具有不同返回和参数的重复方法名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1672742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
so in java you can't have duplicate method names with different return and params?
提问by Ayrad
Is it possible to have two methods with the same name but different parameters and return types in Java? Seems like it would be a good way to generalize a simple getter and setter.. You can do that with constructors why not with regular methods ? for example
Java中是否可以有两个名称相同但参数和返回类型不同的方法?似乎这是概括一个简单的 getter 和 setter 的好方法。你可以用构造函数来做到这一点,为什么不使用常规方法呢?例如
why not be able to do ..
为什么不能做..
int getVal() {
return int;
}
boolean getVal() {
return true;
}
setVal(int a) {
}
and
和
setVal(boolean a) {
}
回答by Brian Agnew
What would you expect if I called:
如果我打电话给你,你会期待什么:
getVal();
with no return vaue being collected ? You have two choices - either the boolean or the integer variant. Since you can't enforce the return value to be collected, the compiler can't determine which variant to be called.
没有回收价值?您有两种选择 - 布尔变量或整数变量。由于您无法强制收集返回值,因此编译器无法确定要调用哪个变体。
You can overload on method parameters, but not on the return types alone, since that's ambiguous (as shown above).
您可以重载方法参数,但不能单独重载返回类型,因为这是不明确的(如上所示)。
回答by Yngve Hammersland
Because then the compiler would be unable to figure out:
因为那样编译器将无法弄清楚:
setVal(getVal());
should it call the bool or int version?
它应该调用 bool 版本还是 int 版本?
回答by Tom Neyland
At first glance it may seem as if there should be no reason that one should not be allowed to do this, but however think about it from the perspective of code that must call this(these) method(s), how would it know which method to invoke?
乍一看似乎没有理由不允许这样做,但是从必须调用此(这些)方法的代码的角度考虑,它如何知道哪个调用方法?
From java.sun.com
The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures. This means that methods within a class can have the same name if they have different parameter lists (there are some qualifications to this that will be discussed in the lesson titled "Interfaces and Inheritance").
Overloaded methods are differentiated by the number and the type of the arguments passed into the method.
You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart.
The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.
Java 编程语言支持方法重载,Java 可以区分具有不同方法签名的方法。这意味着如果类中的方法具有不同的参数列表,则它们可以具有相同的名称(对此有一些限定,将在题为“接口和继承”的课程中讨论)。
重载方法的区别在于传递给方法的参数的数量和类型。
您不能声明多个具有相同名称、相同数量和类型的参数的方法,因为编译器无法区分它们。
编译器在区分方法时不考虑返回类型,因此您不能声明具有相同签名的两个方法,即使它们具有不同的返回类型。
回答by eljenso
As far as the Java Virtual Machineis concerned, it is possible for a class to declare multiple methods with the same signature but different return types.
就Java虚拟机而言,一个类可以声明多个具有相同签名但不同返回类型的方法。
Only, Java as a languageforbids this.
只是,Java 作为一种语言禁止这样做。
回答by St.Shadow
Generally speaking - no. But if you want it very much - then YES :)) Check this great article
一般来说 - 没有。但如果你非常想要它 - 那么是的 :)) 检查这篇很棒的文章
回答by Raze
Different return types, no. But different parameter types / length, yes. That's how Java is... specification says that. They wanted to keep it simple.
不同的返回类型,没有。但是不同的参数类型/长度,是的。Java 就是这样……规范是这么说的。他们想保持简单。
回答by Andrzej Doyle
You candeclare the two setters in your case - try it.
您可以在您的情况下声明两个 setter - 试试看。
Methods must be unique in terms of their name, and the number and type of their arguments. The return type, as well as the throwsclause, do not count in terms of making a method unique (which makes sense, as they're not specified when you invokeit).
方法在名称、参数数量和类型方面必须是唯一的。返回类型以及throws子句在使方法唯一方面不算数(这是有道理的,因为在调用它时未指定它们)。
回答by Oded Peer
According to the Java Language Specification (http://docs.oracle.com/javase/specs/jls/se5.0/html/classes.html#8.4.2):
根据 Java 语言规范 (http://docs.oracle.com/javase/specs/jls/se5.0/html/classes.html#8.4.2):
It is a compile-time error to declare two methods with override-equivalent signatures (defined below) in a class.
Two methods have the same signature if they have the same name and argument types.
在一个类中声明两个具有覆盖等效签名(定义如下)的方法是编译时错误。
如果两个方法具有相同的名称和参数类型,则它们具有相同的签名。

