java_多线程Lamda表达式

Lamda表达式

java_多线程Lamda表达式_第1张图片

java_多线程Lamda表达式_第2张图片

代码演示一

/*
 推导lamda表达式
  */
public class Lamda {
	//3.静态内部类
	static class Like2 implements ILike {

		@Override
		public void lamda() {
			System.out.println("i like lamda2");
		}

	}
	public static void main(String[] args) {
		ILike like = new Like();
		like.lamda();
		like =new Like2();
		like.lamda();
		//局部内部类
		class Like3 implements ILike {
			@Override
			public void lamda() {
				System.out.println("i like lamda3");
			}

		}
		like =new Like3();
		like.lamda();
		//匿名内部类
		like =new ILike() {
			@Override
			public void lamda() {
				System.out.println("i like lamda4");
			}
		};
		like.lamda();
		//用lamda简化
		like =()-> {
			System.out.println("i like lamda5");
		};
		like.lamda();
	}
	
}

//1.定义一个函数式接口
interface ILike {
	void lamda();
}

//2.实现类
class Like implements ILike {

	@Override
	public void lamda() {
		System.out.println("i like lamda");
	}

}

代码演示二

public class lamda2 {

	public static void main(String[] args) {
		ILove love = null;
		/*
		 * ILove love = (int a) -> { System.out.println("i love you->" + a); };
		 */
		
		
		// 简化一
		/*
		 * love = (a) -> { System.out.println("i love you->" + a); };
		 */
		
		
		// 简化二
		/*love = a -> {
			System.out.println("i love you->" + a);
		};
		
		
		 */
		// 简化三
		love = a -> System.out.println("i love you->" + a);
	
		love.love(521);
	}

}

interface ILove {
	void love(int a);
}

你可能感兴趣的:(多线程,Java,java)