Lambda表达式
概念
函数式接口:
一个接口,里面只有一个方法
案例
interface Study {
public void studyChinese();
}
class Student implements Study{
@Override
public void studyChinese() {
System.out.println("学习中文");
}
}
public class Main {
public static void main(String[] args) {
Study study = new Student();
study.studyChinese();
}
}
- 这里存在一个接口里面只有一个接口,里面只有 一个方法
- 这是一个常规的实现
进阶------->静态内部类
public class Main {
static class Student implements Study{
@Override
public void studyChinese() {
System.out.println("学习中文");
}
}
public static void main(String[] args) {
Study study = new Student();
study.studyChinese();
}
}
进阶----->局部内部类
public class Main {
public static void main(String[] args) {
class Student implements Study{
@Override
public void studyChinese() {
System.out.println("学习中文");
}
}
Study study = new Student();
study.studyChinese();
}
}
进阶---->匿名内部类
这个就是实例化接口
public class Main {
public static void main(String[] args) {
Study study = new Study(){
@Override
public void studyChinese() {
System.out.println("学习中文");
}
};
study.studyChinese();
}
}
进阶----->lambda表达式
public class Main {
public static void main(String[] args) {
Study study = ()->{
System.out.println("学习中文");
};
study.studyChinese();
}
}
- 删除了函数的实例化,里面的函数名字都删了 -> 增加了箭头