【发布时间】:2021-10-08 06:06:19
【问题描述】:
我有一个父抽象类和采用泛型的子类。
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>
如何消除这些警告和错误?
【问题讨论】:
-
假设您了解不应使用原始类型(第一次警告的原因),您需要确定的第一件事是
extends Parent<...>中的正确类型参数,并在此过程中回答质疑为什么Parent是通用的。这与密封类无关。解决了这个问题后,您可以尝试回答有关在permits ChildA<...>中设置什么类型参数的密封类问题。这是一个很好的问题,但我没有看到比... permits ChildA<?>, ChildB<?>更有意义的东西。在 JLS 中找不到任何内容。 -
@ernest_k 我从语法中推断出意图。因为它首先不允许在
permits之后使用参数化类型,所以 JLS 作者可能觉得没有必要讨论原始类型与参数化类型。 -
Eclipse 错误报告是here
标签: java eclipse java-17 java-sealed-type