亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么Javac會抱怨與類的類型參數無關的泛型?

為什么Javac會抱怨與類的類型參數無關的泛型?

為什么Javac會抱怨與類的類型參數無關的泛型?請按順序閱讀代碼中的注釋,那里的問題詳細信息。為什么會發生這種差異?如果可能,請引用JLS。import java.util.*;/**  * Suppose I have a generic class  * @param <T> with a type argument.  */class Generic<T> {     // Apart from using T normally,     T paramMethod() { return null; }     // the class' interface also contains Generic Java Collections     // which are not using T, but unrelated types.     List<Integer> unrelatedMethod() { return null; }}@SuppressWarnings("unused")public class Test {     // If I use the class properly (with qualified type arguments)     void properUsage() {         Generic<String> g = new Generic<String>();         // everything works fine.         String s = g.paramMethod();         List<Integer> pos = g.unrelatedMethod();         // OK error: incompatible types: List<String> := List<Integer>         List<String> thisShouldErrorCompile = g.unrelatedMethod();     }     // But when I use the raw type, *ALL* the generics support is gone, even the Collections'.     void rawUsage() {         // Using Generic<?> as the type turns fixes the warnings below.         Generic g = new Generic();         // OK error: incompatible types: String := Object         String s = g.paramMethod();         // WTF warning: unchecked conversion: List<Integer> := raw List         List<Integer> pos = g.unrelatedMethod();         // WTF warning: unchecked conversion: List<String> := raw List         List<String> thisShouldErrorCompile = g.unrelatedMethod();     }}邊注我最初是在IntelliJ IDEA中找到這個的,但是我猜編譯器與javac兼容,因為當我用下面的代碼編譯上面的代碼時,它給出了相同的錯誤/警告。$ javac -version javac 1.7.0_05$ javac Test.java -Xlint:unchecked...$ javac Test.java -Xlint:unchecked -source 1.5 -target 1.5...
查看完整描述

1 回答

?
炎炎設計

TA貢獻1808條經驗 獲得超4個贊

JLS 4.8原始類型開始

僅允許使用原始類型作為對遺留代碼兼容性的讓步。強烈建議不要在將泛型引入Java編程語言后在編寫的代碼中使用原始類型。

未從其超類或超接口繼承的原始類型C的構造函數(第8.8節),實例方法(第8.4節,第9.4節)或非靜態字段(第8.3節)M的類型為與之對應的原始類型在與C對應的通用聲明中刪除其類型。

如果仔細閱讀,這意味著所有類型都將被刪除,而不僅僅是您遺漏的類型。


查看完整回答
反對 回復 2019-09-26
  • 1 回答
  • 0 關注
  • 507 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號