[英]Interface type parameter extends the interface?
I came by this construction when reading a codebase and I can't figure out what it does/represents:我在阅读代码库时遇到了这种结构,但我无法弄清楚它的作用/代表什么:
public interface MyInterface<T extends MyInterface<T>> {}
I don't understand what the type bound does here - it seems almost recursive?我不明白类型绑定在这里做了什么 - 它似乎几乎是递归的? What's really the restriction on
T
in this case?在这种情况下,对
T
的真正限制是什么?
It means that any class that implements the interface must specify T
as themselves:这意味着任何实现该接口的 class 都必须将
T
指定为它们自己:
class MyClass implements MyInterface<MyClass> {}
// │ ↑
// └──────────────────────────────┘
Here T
is MyClass
, which extends MyInterface<MyClass>
, so the T extends MyInterface<T>
bound is satisfied.这里
T
是MyClass
,它扩展MyInterface<MyClass>
,因此满足了T extends MyInterface<T>
界限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.