接口类型参数扩展接口?
Interface type parameter extends the interface?
我在阅读代码库时遇到了这个结构,但我无法弄清楚它是什么does/represents:
public interface MyInterface<T extends MyInterface<T>> {}
我不明白这里的类型绑定是做什么的——它看起来几乎是递归的?在这种情况下,T
的真正限制是什么?
这意味着任何实现该接口的class必须指定T
作为自己:
class MyClass implements MyInterface<MyClass> {}
// │ ↑
// └──────────────────────────────┘
此处 T
是 MyClass
,它扩展了 MyInterface<MyClass>
,因此满足 T extends MyInterface<T>
界限。
我在阅读代码库时遇到了这个结构,但我无法弄清楚它是什么does/represents:
public interface MyInterface<T extends MyInterface<T>> {}
我不明白这里的类型绑定是做什么的——它看起来几乎是递归的?在这种情况下,T
的真正限制是什么?
这意味着任何实现该接口的class必须指定T
作为自己:
class MyClass implements MyInterface<MyClass> {}
// │ ↑
// └──────────────────────────────┘
此处 T
是 MyClass
,它扩展了 MyInterface<MyClass>
,因此满足 T extends MyInterface<T>
界限。