Re: [問題] generic type parameter bound?

看板java作者 (Feng)時間16年前 (2009/05/04 01:42), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串5/8 (看更多)
※ 引述《macbuntu (邀怪)》之銘言: : : 推 TsaiCF:請比較 List<String[]> 跟 List<String>[]. 何者不合法? 05/02 01:19 : : → macbuntu:你寫的這兩個都沒有 type bounds... 05/02 01:23 : : → macbuntu:type bound 是像 <T extends A,B> 中的 A 和 B, 不是 T 05/02 01:25 : : 推 TsaiCF:List<? extends Object[]> z = new ArrayList<String[]>(); 05/02 13:37 : 疑,我沒注意到這個過耶,這樣看 Java parameter type bounds 的規則 : 怎麼變得有點不一致? : class A<T extends Object> { } // OK : class B<T extends Object[]> { } // compile-time error 我想是若 <T> = String[] void foo(T x){ ???? y = x[0]; // ==> 無法判定????是什麼型別. } 另一個常見 的錯誤例子 : class C<T[]> { } 則是若合法可能發生型別錯誤,就不再多提了... : void func() { : A<? extends Object[]> a; // OK : } 因為用 wildcards 不能再叫用 a.foo(T) 之類的, 所以可以. 如下例 : static void showLengths(List<? extends Object[]> x) { for (Object[] array : x) { System.out.println(array.length); } } : 如果 Java 語法可以准許宣告變數 a 時使用 array type 當作 type bound, : 為什麼要在定義型別 B 時不准使用 array type? : 這是有什麼特別的考量嗎? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.132.5.13
文章代碼(AID): #19_TThFn (java)
討論串 (同標題文章)
文章代碼(AID): #19_TThFn (java)