Java Generics Wildcarding With Multiple Classes

后端 未结 3 840
名媛妹妹
名媛妹妹 2020-11-22 08:37

I want to have a Class object, but I want to force whatever class it represents to extend class A and implement interface B.

I can do:

Class

        
3条回答
  •  不思量自难忘°
    2020-11-22 09:17

    You can't do it with "anonymous" type parameters (ie, wildcards that use ?), but you can do it with "named" type parameters. Simply declare the type parameter at method or class level.

    import java.util.List;
    interface A{}
    interface B{}
    public class Test> {
        T t;
    }
    

提交回复
热议问题