为了简化匿名内部类的代码,具体定义:
例如将9.内部类中的匿名内部类例子:
原来代码:
//Main.java
public class Main {
public static void main(String[] args){
ProcessArray ay = new ProcessArray();
int[] array = {1, 2, 3};
ay.process(array, new AddCommand()
{
@Override
public void process(int[] target) {
// TODO Auto-generated method stub
int sum = 0;
for (int tmp : target) {
sum += tmp;
}
System.out.println(sum);
}
});
}
}
Lambda表达式:
//Main.java
public class Main {
public static void main(String[] args){
ProcessArray ay = new ProcessArray();
int[] array = {1, 2, 3};
ay.process(array, /*new AddCommand()*/(int[] target)->{
{
//@Override
//public void process(int[] target) {
// TODO Auto-generated method stub
int sum = 0;
for (int tmp : target) {
sum += tmp;
}
System.out.println(sum);
}
});
}
}