package com.sxt.thread; /** * Lambda表达式 简化线程(用一次)的使用 * */ public class LambdaThread { //静态内部类 static class Test implements Runnable{ public void run() { for(int i=0;i<20;i++) { System.out.println("一边听歌"); } } } public static void main(String[] args) { //new Thread(new Test()).start(); //局部内部类 class Test2 implements Runnable{ public void run() { for(int i=0;i<20;i++) { System.out.println("一边听歌"); } } } new Thread(new Test2()).start(); //匿名内部类 必须借助接口或者父类 new Thread(new Runnable() { public void run() { for(int i=0;i<20;i++) { System.out.println("一边听歌"); } } }).start(); //jdk8 简化 lambda表达式 new Thread(()-> { for(int i=0;i<20;i++) { System.out.println("一边听歌"); } } ).start(); } }
package com.sxt.thread; /** * lambda推导 * * */ public class LambdaTest01 { //内部类 static class Like2 implements ILike{ public void lambda() { System.out.println("i like lambda2 "); } } public static void main(String[] args) { ILike like = new Like(); like.lambda(); like = new Like2(); like.lambda(); class Like3 implements ILike{ public void lambda() { System.out.println("i like lambda3 "); } } like = new Like3(); like.lambda(); //匿名内部类 like = new ILike() { public void lambda() { System.out.println("i like lambda4 "); } }; like.lambda(); //lambda like = ()-> { System.out.println("i like lambda5 "); }; like.lambda(); /* *lambda推导必须存在类型 ()-> { System.out.println("i like lambda5 "); }.lambda(); */ } } interface ILike{ void lambda(); } //外部类 class Like implements ILike{ @Override public void lambda() { System.out.println("i like lambda "); } }
package com.sxt.thread; /** * 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); } }
package com.sxt.thread; /** * lambda推导 +参数+返回值 * * */ public class LambdaTest03 { public static void main(String[] args) { IInterest interest = (int a,int c)-> { System.out.println("i like lambda -->"+(a+c)); return a+c; }; interest.lambda(100,200); interest = (a,c)-> { System.out.println("i like lambda -->"+(a+c)); return a+c; }; interest.lambda(200,200); interest = (a,c)-> { return a+c; }; interest = (a,c)-> a+c; interest = (a,c)-> 100; System.out.println(interest.lambda(10, 20)); } } interface IInterest{ int lambda(int a,int b); } //外部类 class Interest implements IInterest{ @Override public int lambda(int a,int c) { System.out.println("i like lambda -->"+(a+c)); return a+c; } }
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
总的来说,就是
1.(params)->expression
2.(params)->statement
3.(params)->{多行statements}
原文地址:https://www.cnblogs.com/nihaoya-czw/p/10352502.html
时间: 2024-11-06 03:51:03