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

前端 未结 7 2225
深忆病人
深忆病人 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:56

    Your caller knows listener as an ActionListener and therefore it doesn't know anything about that new method. I think the only way to do this (other than doing reflection gymnastics, which really would defeat the purpose of using an anonymous class, i.e. shortcut/simplicity) is to simply subclass ActionListener and not use an anonymous class.

提交回复
热议问题