How to initialize anonymous inner class in Java

后端 未结 4 1781
庸人自扰
庸人自扰 2021-01-08 00:24

Is there any way to initialize anonymous inner class in Java?

For example:

new AbstractAction() {
    actionPerformed(ActionEvent event) {
    ...
          


        
4条回答
  •  难免孤独
    2021-01-08 01:21

    It's not quite clear what you mean, but you can use an initializer block to execute code at construction time:

    new AbstractAction() {
    
        {
            // This code is called on construction
        }
    
        @Override public void actionPerformed(ActionEvent event) {
        ...
        }
    }
    

提交回复
热议问题