Implement two interfaces in an anonymous class

后端 未结 4 1814
南方客
南方客 2021-02-06 21:08

I have two interfaces:

interface A {
    void foo();
}

interface B {
    void bar();
}

I am able to create anonymous instances of classes impl

4条回答
  •  臣服心动
    2021-02-06 21:42

    For save some keystrokes (for example if the interfaces have a lot of methods) you can use

    abstract class Aggregate implements A,B{
    }
    
    new MyObject extends Aggregate{
       void foo(){}
       void bar(){}
    }
    

    Notice the key is to declare the Aggregate as abstract

提交回复
热议问题