作者tkcn (小安)
看板java
标题Re: [问题] Wrapping/Widening + Var-args
时间Wed Sep 8 00:12:59 2010
※ 引述《bennylu (减肥)》之铭言:
: 在设计overloading method时, 如下:
: method(5);
: ..
: void method(long i) {}
: void method(Integer i) {}
: void method(int... i) {}
: 我们知道优先权顺序是widening > wrapping > var-args,
: 但是如果将上述三者做一些组合,
: method(5);
: ..
: void method(long... i) {}
: void method(Integer... i) {}
: 照理说应该是 widening + var-args > wrapping + var-args,
: 但是compiler抱怨说 reference to "method" is ambiguous,
: 所以想来问一下各位先进, compiler在挑选overloaded method时详细的演算法,
: 先谢谢啦
这不能算是演算法,只能算是规则吧。
根据 Java 1.5 Tiger - A Developer's Notebook
method resolution is a three-pass process:
1. The compiler attempts to locate the correct method
without any boxing, unboxing, or vararg invocations.
This will find any method that would have been
invoked under Java 1.4 rules.
2. If the first pass fails, the compiler tries method
resolution again, this time allowing boxing and
unboxing conversions.
Methods with varargs are not considered in this pass.
3. If the second pass fails, the compiler tries method
resolution one last time, allowing boxing and unboxing,
and also considers vararg methods.
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.122.183.195
※ 编辑: tkcn 来自: 140.122.183.195 (09/08 00:13)
※ 编辑: tkcn 来自: 140.122.183.195 (09/08 00:14)
1F:推 bennylu:谢谢学长(误), 不过似乎没有提到为何会ambiguous 09/08 00:16
2F:→ ogamenewbie:单看字面就是进第3阶段, 然後多重符合? 09/08 00:44
3F:→ ogamenewbie:还是你确定那个 wid啥 > 的那个规则在第3阶段也适用? 09/08 00:45
4F:→ tkcn:我的认知就是 2f 那样,也就是你的 "照理说" 是错的。 09/08 09:03
5F:→ bennylu:同意 09/08 11:01
6F:推 dream1124:好问题 推 09/08 11:14