我有一个父类抽象类和一个采用泛型的子类。
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>如何删除这些警告和错误?
发布于 2021-10-08 08:16:46
警告“父类型是原始类型”与密封类完全无关,因为在extends Parent是泛型类时使用Parent<T>会导致这样的警告,因为泛型是存在的。
你很可能想用
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,以消除警告,但由于这会影响整个类,所以我不建议这样做。
https://stackoverflow.com/questions/69491202
复制相似问题