Can I access new methods in anonymous inner class with some syntax?

前端 未结 7 2233
深忆病人
深忆病人 2020-12-01 14:48

Is there any Java syntax to access new methods defined within anonymous inner classes from outer class? I know there can be various workarounds, but I wonder if a special sy

7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 14:57

    Yes you can access the method see the example below if any doubt please comment

    package com;
    interface A
    {
       public void display();
    }
    public class Outer {
        public static void main(String []args)
        {
            A a=new A() {
            @Override
            public void display() {
                System.out.println("Hello");
            }
         };
            a.display();
        }
      }
    

提交回复
热议问题