lambda的演化过程
一、普通的接口实现类
public class TestLambda {
public static void main(String[] args) {
Ilike like = new Like();
like.lambda();
}
}
interface Ilike {
void lambda();
}
class Like implements Ilike {
@Override
public void lambda() {
System.out.println("普通的接口实现类");
}
}
二、静态内部类
public class TestLambda {
static class Like implements Ilike {
@Override
public void lambda() {
System.out.println("静态内部类");
}
}
public static void main(String[] args) {
Ilike like = new Like();
like.lambda();
}
}
interface Ilike {
void lambda();
}
三、方法里的局部内部类
public class TestLambda {
public static void main(String[] args) {
class Like implements Ilike {
@Override
public void lambda() {
System.out.println("方法里的局部内部类");
}
}
Ilike like = new Like();
like.lambda();
}
}
interface Ilike {
void lambda();
}
四、匿名内部类
public class TestLambda {
public static void main(String[] args) {
Ilike like = new Ilike() {
@Override
public void lambda() {
System.out.println("匿名内部类");
}
};
like.lambda();
}
}
interface Ilike {
void lambda();
}
五、lambda表达式
public class TestLambda {
public static void main(String[] args) {
Ilike like = () -> System.out.println("匿名内部类");
like.lambda();
}
}
interface Ilike {
void lambda();
}