书上说抽象类和接中不能用new实例化,但是ActionListener是接口,WindowAdapter是抽象类,在很多事件处理时都用到了new,这是怎么回事?

解决方案 »

  1.   

    WindowAdapter不是真正的抽象类  里面实现了空的方法抽象类和接口不能实例化
      

  2.   

    WindowAdapter是个适配器,它将监听接口中的方法以空方法的形式来实现,它是一个具有空方法的实体类,所以可以new
      

  3.   

    是说不能建立它们的实例,就是说不能直接用Interface interface= new InterfaceObject();
    而可以InterfaceObject interface= new InterfaceImp();
    既是说把其的reference定位到这个interface or abstract class的implemeted class的实例。
      

  4.   

    楼主大哥,可能看到有这样的写法,所以就认为接口或抽象类也能"new " 他一把了.button.addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent e){ 
             System.exit(1); 
        } 
    }); 其实这是个匿名类呀! 所谓匿名就是这个类连个名字都没有,但他却implements了 ActionListener罢了.WindowAdapter是个适配器,我猜测你看到应该也是一匿名类的写法.