简体   繁体   中英

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?

It means that any class that implements the interface must specify T as themselves:

class MyClass implements MyInterface<MyClass> {}
//       │                              ↑
//       └──────────────────────────────┘

Here T is MyClass , which extends MyInterface<MyClass> , so the T extends MyInterface<T> bound is satisfied.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM