我有一個父抽象類和采用泛型的子類。
public abstract sealed class Parent<T> permits ChildA, ChildB {}
public non-sealed class ChildA<T extends FileTypeA> extends Parent{}
public non-sealed class ChildB<T extends FileTypeB> extends Parent{}
在父類中,我收到警告:
ChildA is a raw type. References to generic type ChildA<T>
should be parameterized
ChildB is a raw type. References to generic type ChildB<T>
should be parameterized
在子課程中,我收到警告:
Parent is a raw type. References to generic type Parent<T>
should be parameterized
使它們像這樣引數化:
public abstract sealed class Parent<T>
permits ChildA<T extends FileTypeA>, ChildB<T extends FileTypeB> {}
甚至
public abstract sealed class Parent<T>
permits ChildA<T>, ChildB<T> {}
給出錯誤:
Bound mismatch: The type T is not a valid substitute for the
bounded parameter <T extends FileTypeA> of the type ChildA<T>
如何洗掉這些警告和錯誤?
uj5u.com熱心網友回復:
警告“ Parent is a raw type ”與密封類完全無關,因為使用extends Parentwhen Parent<T>is a generic class 會導致這樣的警告,因為泛型存在。
你很可能想使用
public non-sealed class ChildA<T extends FileTypeA> extends Parent<T> {}
public non-sealed class ChildB<T extends FileTypeB> extends Parent<T> {}
另一個問題似乎是 Eclipse 錯誤,因為我只能在那里重現警告。當我將宣告更改為 時permits ChildA<?>, ChildB<?>,警告消失,但您不應該這樣做。
在Java語言規范定義了permits子句
ClassPermits:
permits TypeName {, TypeName}
而TypeName 鏈接到
TypeName:
TypeIdentifier
PackageOrTypeName . TypeIdentifier
PackageOrTypeName:
Identifier
PackageOrTypeName . Identifier
這顯然會導致一系列沒有任何型別引數的點分隔識別符號。一致地,javac拒絕像permits ChildA<?>, ChildB<?>.
換句話說,Eclipse 不應在此處生成警告,更重要的是,不應在permit子句中接受引數化型別。您最好的選擇是等待 Eclipse 的 Java 17 支持的修復。您可以@SuppressWarnings("rawtypes")在整個Parent班級中添加 a以使警告消失,但由于這會影響整個班級,因此我不建議這樣做。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/324396.html
