Java - Defining a member that extends class A and implements interface B

前端 未结 2 834
悲哀的现实
悲哀的现实 2020-12-21 17:30

I have a variable that must meet two conditions, and I want to set them in the definition

I know that I can define either condition with an individual variable, like

2条回答
  •  粉色の甜心
    2020-12-21 17:58

    You can declare type parameters that have multiple bounds, such as:

    public static  void test(Class clazz)
    

    But you cannot declare a variable that has multiple bounds:

    private Class variable;  // doesn't work
    

    You can create an abstract class C that extends A and implements B, so that only one bound is required.

    abstract class C extends A implements B {}
    

    Then:

    private Class variable;
    

提交回复
热议问题