Il libro che sto leggendo su Java afferma qualcosa di confuso e inaccettabile.
Learning About Ambiguity
When you overload methods, you risk creating an ambiguous situation - one which the compiler cannot determine which method to use. For example, consider the following overloaded
computeBalance()
method declarations:public static void computeBalance(double deposit) public static void computeBalance(double withdrawal)
If you declare a
double
variable namedmyDeposit
and make a method call such ascomputeBalance(myDeposit);
, you will have created an ambiguous situation. Both methods are exact matches for your call. You might argue that a call using a variable namedmyDeposit
"seems" like it should go to the version of the method with the parameter nameddeposit
, but Java makes no assumptions based on variable names. Each version ofcomputeBalance()
could accept adouble
, and Java does not presume which one you intend to use.
Questo viola le regole per sovraccaricare un metodo. Come può essere sovraccaricato un metodo con la stessa lista di parametri? Non è impossibile o non sto ottenendo qualcosa? Ho provato e compilato un codice simile, restituisce il seguente errore (che ha senso):
method computeBalance() is already defined in class XXX