Java Generics Wildcarding With Multiple Classes

后端 未结 3 842
名媛妹妹
名媛妹妹 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:16

    Actually, you can do what you want. If you want to provide multiple interfaces or a class plus interfaces, you have to have your wildcard look something like this:

    
    

    See the Generics Tutorial at sun.com, specifically the Bounded Type Parameters section, at the bottom of the page. You can actually list more than one interface if you wish, using & InterfaceName for each one that you need.

    This can get arbitrarily complicated. To demonstrate, see the JavaDoc declaration of Collections#max, which (wrapped onto two lines) is:

    public static > T
                                               max(Collection coll)
    

    why so complicated? As said in the Java Generics FAQ: To preserve binary compatibility.

    It looks like this doesn't work for variable declaration, but it does work when putting a generic boundary on a class. Thus, to do what you want, you may have to jump through a few hoops. But you can do it. You can do something like this, putting a generic boundary on your class and then:

    class classB { }
    interface interfaceC { }
    
    public class MyClass {
        Class variable;
    }
    

    to get variable that has the restriction that you want. For more information and examples, check out page 3 of Generics in Java 5.0. Note, in , the class name must come first, and interfaces follow. And of course you can only list a single class.

提交回复
热议问题