代码如下:
package LambdaPracticePack;public interface Eatable {void eat();
}
package LambdaPracticePack;public class EatableImpl implements Eatable{@Overridepublic void eat() {System.out.println("一天一苹果,医生远离我");}
}
package LambdaPracticePack;public class EatableDemo {public static void main(String[] args){Eatable e = new EatableImpl();useEatable(e);useEatable(new Eatable() {@Overridepublic void eat() {System.out.println("一天一苹果,医生远离我");}});useEatable(()->{System.out.println("一天一苹果,医生远离我");});}private static void useEatable(Eatable e){e.eat();}
}
代码如下:
package LambdaPracticePack;public class FlyableDemo {public static void main(String[] args){useLlyable(new Flyable() {@Overridepublic void fly(String s) {System.out.println(s);System.out.println("飞机自驾游");}});System.out.println("-----------------------------");useLlyable((String s)->{System.out.println(s);System.out.println("飞机自驾游");});}private static void useLlyable(Flyable f){f.fly("风和日丽,晴空万里");}
}
代码如下:
package LambdaPracticePack;public interface Addable {int add(int a,int b);
}
package LambdaPracticePack;public class AddableDemo {public static void main(String[] args){useAddable((int a,int b)->{return a+b;});}private static void useAddable(Addable a){int sum = a.add(10, 20);System.out.println(sum);}}