简体   繁体   English

接口类型参数扩展接口?

[英]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.这里TMyClass ,它扩展MyInterface<MyClass> ,因此满足了T extends MyInterface<T>界限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实现接口的解决方法包括<TIn extends T> , 带 X[] 类型参数 - Workarounds to implement interface including <TIn extends T>, with X[] type parameter Java GenericClass&lt; ? 扩展接口&gt;作为参数 - Java GenericClass< ? extends Interface> as a Parameter 我如何在java中使用一个接受扩展接口类型的参数的函数来创建接口? - How can i make an interface in java with a function that accepts a parameter of the type that extends the interface? 为什么任何类型的EXTENDS接口? - Why any type can EXTENDS interface? Java返回扩展接口的泛型类型 - Java returning generic type that extends an interface Java如何返回扩展接口的泛型类型 - Java how to return a generic type that extends an interface 您如何强制方法参数类型扩展特定类并实现特定接口? - How do you enforce that a method parameter type extends a specific class and implements a specific interface? 如何创建具有扩展另一个类并实现接口的类型参数的基适配器? - How to create base adapter which has type parameter that extends another class and implements an interface? 在没有接口类型参数的情况下实现接口 - Implementing an interface without an interface type parameter Java为什么接口扩展接口 - Java why interface extends interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM