How to implement an interface with an enum, where the interface extends Comparable?

。_饼干妹妹 提交于 2019-11-30 12:35:26

Enums implement Comparable, so FooImpl ends up extending Comparable twice with incompatible arguments.

The following will work:

public interface Foo<SelfType extends Foo<SelfType>> extends Comparable<SelfType> { ... }

public enum FooImpl implements Foo<FooImpl> { ... }
NimChimpsky

Enum already implements comparable so you can't override it.

A general answer regarding why-would-an-enum-implement-an-interface.

Actually the error you will get is :

The interface Comparable cannot be implemented more than once with different arguments : Comparable<FooImpl> and Comparable<Foo>

As enum FooImpl already implementing Comparable<FooImpl> implicitly, you can not override it again as Comparable<Foo>.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!