简体   繁体   中英

How do I declare a class that implements an interface that constrains the type of a class?

I've seen examples of how to declare a generic class whose type parameter is constrained, eg it must extend Comparable. I also know how to define an interface for a generic class whose type is similarly constrained. However, when I do the latter, I'm unable to figure out the syntax for the class signature. So, to cut a long story short, if the interface is

public interface iMyClass<T extends Comparable<T>>

what should be the syntax for the signature for the implementing class

public class MyClass.......

Thanks in advance, D

Do you intend MyClass to be generic as well? If so,

public class MyClass<? extends Comparable<T>> implements iMyClass<T> {

Otherwise, keep things simple and

public class MyClass implements iMyClass<String> {

Replace String with your Comparable .

约束类型参数的提供方式与非约束类型参数的提供方式完全相同,例如:

public class MyClass implements iMyClass<String> {

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