package org.example;public class TestLambda {public static void main(String[] args) {Like like=new Like();like.lambda();}}//定义一个函数式接口
interface ILike{void lambda();
}//实现类
class Like implements ILike{@Overridepublic void lambda() {System.out.println("我喜欢学习");}
}
将实现类放进内部类中
package org.example;public class TestLambda {static class Like2 implements ILike{@Overridepublic void lambda() {System.out.println("我喜欢美食");}}public static void main(String[] args) {ILike like=new Like();like.lambda();like= new Like2();like.lambda();}}//定义一个函数式接口
interface ILike{void lambda();
}//实现类
class Like implements ILike{@Overridepublic void lambda() {System.out.println("我喜欢学习");}
}
局部内部类
package org.example;public class TestLambda {static class Like2 implements ILike{@Overridepublic void lambda() {System.out.println("我喜欢美食");}}public static void main(String[] args) {ILike like=new Like();like.lambda();like= new Like2();like.lambda();class Like3 implements ILike{@Overridepublic void lambda() {System.out.println("我喜欢电影");}}like=new Like3();like.lambda();}}//定义一个函数式接口
interface ILike{void lambda();
}//实现类
class Like implements ILike{@Overridepublic void lambda() {System.out.println("我喜欢学习");}
}
匿名内部类
package org.example;public class TestLambda {//静态内部类static class Like2 implements ILike{@Overridepublic void lambda() {System.out.println("我喜欢美食");}}public static void main(String[] args) {ILike like=new Like();like.lambda();like= new Like2();like.lambda();//局部内部类class Like3 implements ILike{@Overridepublic void lambda() {System.out.println("我喜欢电影");}}like=new Like3();like.lambda();//匿名内部类like=new ILike() {@Overridepublic void lambda() {System.out.println("我喜欢逛街");}};like.lambda();}}//定义一个函数式接口
interface ILike{void lambda();
}//实现类
class Like implements ILike{@Overridepublic void lambda() {System.out.println("我喜欢学习");}
}
like=new ILike() {@Overridepublic void lambda() {System.out.println("我喜欢逛街");}};like.lambda();
lambda简化
package org.example;public class TestLambda {//静态内部类static class Like2 implements ILike{@Overridepublic void lambda() {System.out.println("我喜欢美食");}}public static void main(String[] args) {ILike like=new Like();like.lambda();like= new Like2();like.lambda();//局部内部类class Like3 implements ILike{@Overridepublic void lambda() {System.out.println("我喜欢电影");}}like=new Like3();like.lambda();//匿名内部类like=new ILike() {@Overridepublic void lambda() {System.out.println("我喜欢逛街");}};like.lambda();//用lambda简化like=()->{System.out.println("我喜欢音乐");};like.lambda();}}//定义一个函数式接口
interface ILike{void lambda();
}//实现类
class Like implements ILike{@Overridepublic void lambda() {System.out.println("我喜欢学习");}
}
like=()->{System.out.println("我喜欢音乐");};like.lambda();
去掉花括号
//去掉花括号like=() -> System.out.println("我喜欢画画");like.lambda();