静态代理
要点:
公共接口
真实角色 代理角色
public class StaticProxy { public static void main(String[] args) { You you = new You(); WeddingCompany com = new WeddingCompany(you); com.happyMarry(); System.out.println("####################"); new WeddingCompany(new You()).happyMarry(); //new Thread(线程对象).start(); } }interface Marry{ void happyMarry();}class You implements Marry{ @Override public void happyMarry() { System.out.println("我花开尽百花杀"); }}// 代理角色class WeddingCompany implements Marry { // 真实角色 private Marry target; public WeddingCompany(Marry target) { super(); this.target = target; } @Override public void happyMarry() { ready(); this.target.happyMarry(); after(); } private void ready() { System.out.println("待到秋来九月八"); } private void after() { System.out.println("冲天香阵透长安"); }}/*待到秋来九月八我花开尽百花杀冲天香阵透长安####################待到秋来九月八我花开尽百花杀冲天香阵透长安满城尽带黄金甲(这句是来凑数的)*/
lambda
使用lambda可避免匿名内部类定义过多
实质输入函数式编程的概念(params) -> expression(params) -> statement(params) -> {statements}
/***************匿名内部类与接口同名时*************//** * 匿名内部类与接口同名 * @author Administrator * */public class TestHaHa { public static void main(String[] args) { ILike_1 like = new ILike_1() { public void lambda() { System.out.println("I like lambda(匿名内部类)."); } }; like.lambda(); // 这个和接口同名匿名内部类必须实现接口中的方法, 否则报错// ILike_1 like_1 = new ILike_1() {// public void lambda_1() {// System.out.println("I like lambda(匿名内部类).");// }// }; }}interface ILike_1{ void lambda();}/*I like lambda(匿名内部类).*/
/**************lambda无参数形式**************/public class LambdaTest01 { // 静态内部类 static class Like2 implements ILike{ @Override public void lambda() { System.out.println("I like lambda(静态内部类)."); } } public static void main(String[] args) { ILike like = new Like(); like.lambda(); like = new Like2(); like.lambda(); class Like3 implements ILike{ @Override public void lambda() { System.out.println("I like lambda(方法内部类)."); } } like = new Like3(); like.lambda(); // 匿名内部类 like = new ILike() { public void lambda() { System.out.println("I like lambda(匿名内部类)."); } }; like.lambda(); // lambda, like = ()->{ System.out.println("I like lambda(lambda)."); }; like.lambda(); /********lambda推到必须存在类型***************** ()->{ System.out.println("I like lambda(lambda)."); }; like.lambda(); */ }}interface ILike{ void lambda();} // 外部类class Like implements ILike{ @Override public void lambda() { System.out.println("I like lambda(外部类)."); }}/*I like lambda(外部类).I like lambda(静态内部类).I like lambda(方法内部类).I like lambda(匿名内部类).I like lambda(lambda).*/
/************一个形参**************/public class LambdaTest02 { public static void main(String[] args) { ILove love = (int a) -> { System.out.println("I like lambda(外部类)---->" + a); }; love.lambda(100); // 简化 love = (a) -> { System.out.println("I like lambda(外部类)---->" + a); }; love.lambda(50); // 简化, 只有一个参数时 love = a -> { System.out.println("I like lambda(外部类)---->" + a); }; love.lambda(5); // 简化, 只有一行代码时, 多行代码不能简化 love = a -> System.out.println("I like lambda(外部类)---->" + a); love.lambda(0); }}interface ILove{ void lambda(int a);} // 外部类class Love implements ILove{ @Override public void lambda(int a) { System.out.println("I like lambda(外部类)---->" + a); }}/*I like lambda(外部类)---->100I like lambda(外部类)---->50I like lambda(外部类)---->5I like lambda(外部类)---->0*/
/************形参+返回值**************/public class LambdaTest03 { public static void main(String[] args) { IInterest interest = (int a1, int b1) -> { System.out.println("I like lambda(外部类)---->" + a1 + b1); return a1 + b1; }; System.out.println(interest.lambda(100, 200)); // 简化 interest = (a1, b1) -> { System.out.println("I like lambda(外部类)---->" + a1 + b1); return a1 + b1; }; interest.lambda(100, 40); interest = (a, c) -> {return a + c;}; interest = (a, c) -> a + c; interest = (a, c) -> 0; System.out.println(interest.lambda(10, 29)); }}interface IInterest{ int lambda(int a, int b);} // 外部类class Interest implements IInterest{ @Override public int lambda(int a1, int b1) { System.out.println("I like lambda(外部类)---->" + a1 + b1); return a1 + b1; }}/*I like lambda(外部类)---->100200300I like lambda(外部类)---->100400*/
/*****************还能这么玩*****************/public class TestHaHa { public static void main(String[] args) { ILike_1 like_1 = () -> { System.out.println("haha"); }; like_1.haha(); }}interface ILike_1{ void haha();}/*haha*/
/************Thread + Lambda**************/public class LambdaTest04 { public static void main(String[] args) { new Thread(()->{ System.out.println("学习lambda"); }).start(); new Thread(()->System.out.println("学习lambda")).start(); }}/*学习lambda学习lambda*/