//Lamda表达式publicclassLambaDemo{publicstaticvoidmain(String[] args){ILike test=newLike();test.Lamda();newLike2().Lamda();//局部内部类classLike3implementsILike{@OverridepublicvoidLamda(){System.out.println("i like Lamda3");}}newLike3().Lamda();//匿名内部类 没有类的名称,必须借助接口或继承ILike like4=newILike(){@OverridepublicvoidLamda(){System.out.println("i like Lamda4");}};like4.Lamda();//Lamda简化ILike like5=()->{System.out.println("i like Lamda5");};like5.Lamda();}//静态内部类staticclassLike2implementsILike{@OverridepublicvoidLamda(){System.out.println("i like Lamda2");}}}//定义函数式接口interfaceILike{voidLamda();}//实现类classLikeimplementsILike{@OverridepublicvoidLamda(){System.out.println("i like Lamda");}}
带参数的Lamda及优化
//带参数的LambapublicclassLamdaDemo2{publicstaticvoidmain(String[] args){//匿名内部类// Love people2=new Love(){// @Override// public void sayLover(String name) {// System.out.println(name+"say:i love you");// }// };//Lamba表达式// Love people2=(String name)-> {// System.out.println(name+"say:i love you");// };//简化Lamda:取参数类型(多个参数也可以去掉参数类型,要去掉就都去掉,必须加括号)// Love people2=(name)-> {// System.out.println(name+"say:i love you");// };//简化Lamda:参数去掉括号// Love people2=name-> {// System.out.println(name+"say:i love you");// };//简化Lamda:去掉花括号:前提:代码只有一行Love people2=name->System.out.println(name+"say:i love you");
people2.sayLover("ls");//总结://前提:接口为函数式接口(接口中只有一个方法)}}interfaceLove{voidsayLover(String name);}
多线程静态代理//静态代理总结://真实对象和代理对象都要实现同一个接口//代理对象要代理真实角色:把真实对象通过参数传给代理对象//好处://代理对象可以做好多真实对象做不了的事情//真实对象专注做自己的事情 public class StaticProxy { public static void main(String[] args) { WeddingCompany company=new WeddingCompany(new You());