抽象类和接口不能实例化吗? 书上说抽象类和接中不能用new实例化,但是ActionListener是接口,WindowAdapter是抽象类,在很多事件处理时都用到了new,这是怎么回事? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 WindowAdapter不是真正的抽象类 里面实现了空的方法抽象类和接口不能实例化 WindowAdapter是个适配器,它将监听接口中的方法以空方法的形式来实现,它是一个具有空方法的实体类,所以可以new 是说不能建立它们的实例,就是说不能直接用Interface interface= new InterfaceObject();而可以InterfaceObject interface= new InterfaceImp();既是说把其的reference定位到这个interface or abstract class的implemeted class的实例。 楼主大哥,可能看到有这样的写法,所以就认为接口或抽象类也能"new " 他一把了.button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ System.exit(1); } }); 其实这是个匿名类呀! 所谓匿名就是这个类连个名字都没有,但他却implements了 ActionListener罢了.WindowAdapter是个适配器,我猜测你看到应该也是一匿名类的写法. java与C我先学哪个 苦恼的java问题~~ 如何将图片黑白化 100分求问/回答即给分 一个读写文件ini的类,但是读写时有时会莫名其妙地多加回车换行 ActionListener 不是类? 求助!!哪位能帮小弟解决一下关于java和短信的问题(分我给) Java程序如何打印报表? 【Java------能解决这个问题吗???】 一个java小程序问题求助 请问怎样把BufferedImage里面的东西转成Image输出! 北京到那里学java好
而可以InterfaceObject interface= new InterfaceImp();
既是说把其的reference定位到这个interface or abstract class的implemeted class的实例。
public void actionPerformed(ActionEvent e){
System.exit(1);
}
}); 其实这是个匿名类呀! 所谓匿名就是这个类连个名字都没有,但他却implements了 ActionListener罢了.WindowAdapter是个适配器,我猜测你看到应该也是一匿名类的写法.