开发者

so in java you can't have duplicate method names with different return and params?

开发者 https://www.devze.com 2022-12-10 16:44 出处:网络
Is it pos开发者_JAVA技巧sible 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

Is it pos开发者_JAVA技巧sible 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

why not be able to do ..

int getVal() {

return int;
}

boolean getVal() {

return true;

}

setVal(int a) {
}

and

setVal(boolean a) {

}


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).


Because then the compiler would be unable to figure out:

setVal(getVal());

should it call the bool or int version?


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.


As far as the Java Virtual Machine is concerned, it is possible for a class to declare multiple methods with the same signature but different return types.

Only, Java as a language forbids this.


Different return types, no. But different parameter types / length, yes. That's how Java is... specification says that. They wanted to keep it simple.


Generally speaking - no. But if you want it very much - then YES :)) Check this great article


You can declare the two setters in your case - try it.

Methods must be unique in terms of their name, and the number and type of their arguments. The return type, as well as the throws clause, do not count in terms of making a method unique (which makes sense, as they're not specified when you invoke it).


According to the Java Language Specification (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.

0

精彩评论

暂无评论...
验证码 换一张
取 消