soc*_*soc 11 java generics enums interface comparable
考虑以下代码:
public interface Foo extends Comparable<Foo> {}
public enum FooImpl implements Foo {}
Run Code Online (Sandbox Code Playgroud)
由于类型擦除的限制,我收到以下错误:
java.lang.Comparable不能用不同的参数继承:
<Foo>
和<FooImpl>
我有以下要求:
FooImpl
需要是一个枚举,因为我需要将它用作注释中的默认值.我已经尝试在接口中使用泛型边界,但Java不支持.
jvd*_*ste 12
枚举实现Comparable,因此FooImpl最终使用不兼容的参数扩展Comparable两次.
以下将有效:
public interface Foo<SelfType extends Foo<SelfType>> extends Comparable<SelfType> { ... }
public enum FooImpl implements Foo<FooImpl> { ... }
Run Code Online (Sandbox Code Playgroud)