Lambda表达式的使用前提:
**1.**有一个接口
**2.**接口中有且仅有一个抽象方法
练习描述 :
无参无返回值抽象方法的练习
操作步骤:
**1.**定义一个接口(Eatable),里面定义一个抽象方法:void eat();
**2.**定义一个测试类(EatableDemo),在测试类中提供两个方法:
A:一个方法是:useEatable(Eatable e)
B:一个方法是主方法,在主方法中调用useEatable方法
代码:
//接口
public interface Eatable {
void eat();
}
实现类:
public class EatableImpl implements Eatable {
@Override
public void eat() {
System.out.println("在干嘛?");
}
}
测试类:
public class EatableDemo {
public static void main(String[] args) {
方式1 在主方法中调用useEatable方法
Eatable e = new EatableImpl();
useEatable(e);
方式2 匿名内部类
useEatable(new EatableImpl(){
@Override
public void eat() {
System.out.println("多喝水");
}