Re: [問題] Wrapping/Widening + Var-args

看板java作者 (小安)時間15年前 (2010/09/08 00:13), 編輯推噓2(204)
留言6則, 4人參與, 最新討論串2/5 (看更多)
※ 引述《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)

09/08 00:16, , 1F
謝謝學長(誤), 不過似乎沒有提到為何會ambiguous
09/08 00:16, 1F

09/08 00:44, , 2F
單看字面就是進第3階段, 然後多重符合?
09/08 00:44, 2F

09/08 00:45, , 3F
還是你確定那個 wid啥 > 的那個規則在第3階段也適用?
09/08 00:45, 3F

09/08 09:03, , 4F
我的認知就是 2f 那樣,也就是你的 "照理說" 是錯的。
09/08 09:03, 4F

09/08 11:01, , 5F
同意
09/08 11:01, 5F

09/08 11:14, , 6F
好問題 推
09/08 11:14, 6F
文章代碼(AID): #1CXcIExV (java)
討論串 (同標題文章)
文章代碼(AID): #1CXcIExV (java)