我正在阅读以下新功能: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");
}
});
Run Code Online (Sandbox Code Playgroud)
使用Lambda:
button.addActionListener(e -> {
System.out.println("Action Detected");
});
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
... 等等?
在Java 8 中引入了lambda表达式以帮助减少样板代码.如果界面只有一个方法,它可以正常工作.如果它由多个方法组成,那么这些方法都不起作用.我该如何处理多种方法?
我们可以选择以下示例
public interface I1()
{
void show1();
void show2();
}
Run Code Online (Sandbox Code Playgroud)
那么主要功能的结构是什么来定义main本身的方法呢?