我的情况是不同的,但作为一个例子如何限制普通类型的接口只接受实施者类
public interface Comparable<T>
会允许我说:
public class MyType implements Comparable<OtherType>
但这是很少你想要什么实现。
有没有办法来力我说:
public class MyType implements Comparable<MyType>
我得到的最接近是:
public interface Comparable<T extends Comparable<T>>
这只能部分,因为它不会允许OtherType
如果它本身不具有可比性,但会允许:
public class MyType implements Comparable<Integer>
满足条件。
无法在Java中完成。至少不是在编译时。 – shmosel
也许如果你说为什么你需要在编译时执行这样的限制,我们可以拿出不同的设计。请参阅http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – SergGr
@SergGr简单地减少运行时检查。该接口的默认方法会执行整个注释处理,并严格要求它们具有相同类型(或至少是一个子类型)。 – Mordechai