创建实现 ActionListener 接口的嵌套类有哪些好处或坏处:
public class Foo{
Foo(){
something.addActionListener(new ButtonListener());
}
//...
private class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
//...
}
}
}
与在主类本身实现ActionListener相比:
public class Foo implements ActionListener{
Foo(){
something.addActionListener(this);
}
//...
public void actionPerformed(ActionEvent e){
//...
}
}
我经常看到这两个例子,只想知道是否有“最佳实践”。