Lambda寫法和傳統寫法對比

import java.util.function.Function;

public class FunctionApply {
	public static void main(String[] args) {
		/*Lambda寫法
		 * 1,定接口,創建一個方法如,demo,把對應的接口Function作爲參數列表傳遞,調用該接口Function中的
		 * 	   抽象方法apply,進行邏輯處理。
		 * private static void demo(Function<String, Integer> function) {
			Integer num = function.apply("20");//R apply(T t);R是t返回的結果
			System.out.println(num);
		   }
		 * 2,調用方法名demo(),在括號內進行Lambda表達式的邏輯處理(也就是傳參,規則處理)
			demo(s -> Integer.parseInt(s));
		 *  */
		demo(s -> Integer.parseInt(s));
		//傳統寫法
		/* 1,建類MyApply,實現Function接口,覆蓋重寫方法apply
		 * class MyApply implements Function<String, Integer>{
			public Integer apply(String t) {
				return Integer.parseInt(t);
			}}
		 * 2,創建對象new,用對象名調用(.)覆蓋重寫的方法apply
}
		 * 3,返回結果,打印
		 * 
		 * */
		MyApply myApply = new MyApply();
		Integer nu = myApply.apply("333");
		System.out.println(nu);
	}
	private static void demo(Function<String, Integer> function) {
		Integer num = function.apply("20");//R apply(T t);R是t返回的結果
		System.out.println(num);
	}
}
class MyApply implements Function<String, Integer>{
	@Override
	public Integer apply(String t) {
		return Integer.parseInt(t);
	}
}

小小Demo,隨記

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章