Java 8 Lambda 表达式 - 嵌套类中的多个方法怎么样
2022-08-31 11:00:44
我正在阅读有关新功能的信息:http://www.javaworld.com/article/2078836/java-se/love-and-hate-for-java-8.html
我看到了下面的例子:
使用匿名类:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println("Action Detected");
}
});
使用 Lambda:
button.addActionListener(e -> {
System.out.println("Action Detected");
});
如果有人想在匿名类中实现多个方法,他们会怎么做,例如:MouseListener
public void mousePressed(MouseEvent e) {
saySomething("Mouse pressed; # of clicks: "
+ e.getClickCount(), e);
}
public void mouseReleased(MouseEvent e) {
saySomething("Mouse released; # of clicks: "
+ e.getClickCount(), e);
}
...等等?