pet*_*ter 1 java interface anonymous-function inner-classes
在处理匿名内部类时,我对接口的概念感到困惑.据我所知,您无法在JAVA中实例化接口,因此以下语句会产生编译错误
ActionListener action = new ActionListener(); // compile error
Run Code Online (Sandbox Code Playgroud)
但是当它处理匿名类时会发生什么?为什么允许使用新的?例如
JButton button = new JButton("A");
button.addActionListener(new ActionListener(){ //this is fine
@Override
public void actionPerformed(ActionEvent e){
}
};
Run Code Online (Sandbox Code Playgroud)
编译器只是ActionListener在场景后面创建一个类并实现吗?它是如何工作的 ?