Something about overloaded

1.Distinguishing overloaded methods

If the methods have the same name, how can Java know which method you mean?There is a simple rule: Each overloaded method must take a unique list of argument types.

2.Overloading on return values

It is common to wander,"Why only class names and methods arguments lists? Why not distinguish between methods based on their return values?" For example ,these two methods ,which have the same name and arguments,are easily distinguished from each other:

void f() {}

int f() {return 1;}

This might work fine as long as the compiler could unequivocally determine the meaning from the context ,as in int x = f().However ,you can also call a method and ignore the return value. This is often referred to as calling a method for side effect, since you don't care about the return value,but instead want the other effects of the method call. So if you call the method this way:

f();

how can Java determine which f() should be called? And how could someone reading the code see it? Because of this sort of problem, you cannot use return value types to distinguish overloaded methods.

posted @ 2013-01-16 16:37  siyusiying  阅读(99)  评论(0编辑  收藏  举报