public class LambdaTest1 {
// 2静态内部类
static class Like2 implements LikeMe {
@Override
public void lambda() {
System.out.println("我喜欢用lambda表达式2");
}
}
public static void main(String[] args) {
// 1 调用接口实现类
LikeMe like = new Like();
like.lambda();
// 2静态内部类
Like2 like2 = new Like2();
like2.lambda();
// 3局部内部类
class Like3 implements LikeMe {
@Override
public void lambda() {
System.out.println("我喜欢用lambda表达式3");
}
}
Like3 like3 = new Like3();
like3.lambda();
// 4匿名内部类,没有类的名称,必需借助父级和接口
LikeMe like4= new LikeMe() {
@Override
public void lambda() {
System.out.println("我喜欢用lambda表达式4");
}
};
like4.lambda();
// lambda表达式,这里只有一个接口和方法,故用lambda,用于:只有一个接口和方法
Runnable like5 = () -> {
System.out.println("我喜欢用lambda表达式5");
};
// 直接跑这个表达式
like5.run();
LikeMe like6 = () -> {
System.out.println("我喜欢用lambda表达式6");
};
like6.lambda();
}
}
//定义一个接口
interface LikeMe {
void lambda();
}
//实现类
class Like implements LikeMe {
@Override
public void lambda() {
System.out.println("我喜欢用lambda表达式1");
}
}
Lamdba推导
最新推荐文章于 2024-08-15 14:47:41 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)