需求:
给定一个厨子cook接口,内含唯一的抽象方法makeFood,且无参数、无返回值。
使用Lambda的标准格式调用invokeCook方法,打印输出“吃饭啦!”字样
java
public class Demo01Cook { public static void main(String[] args) { //调用invoke Cook方法,参数是Cook接口,传递Cook接口匿名内部类对象 invokeCook(new Cook() { @Override public void makeFood() { System.out.println("吃饭啦"); } }); //使用Lambda表达式,简化匿名内部类方法 invokeCook(()->{ System.out.println("吃饭啦1"); }); } //定义一个方法,参数传递cook接口,方法内部调用Cook接口中的方法makefood public static void invokeCook(Cook cook){ cook.makeFood(); } }
interface接口:
public interface Cook { public abstract void makeFood(); }