package com.sxt.thread; /** * lambda推导 +参数+返回值 * @author:裴新 * */ 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; } }
package com.sxt.thread; /** * Lambda表达式 简化线程(用一次)的使用 * @author 裴新 * */ 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(); } }
原文:https://www.cnblogs.com/skyline1/p/11198002.html