jdk8四種函數式接口

/**
 * 
 */
package com.gewb;

/**
 * @author Bingo.Ge
 * @date 2020年6月6日
 */
public class TestDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		// 即使用string的static方法valueOf來當作IMessage接口的實現
//		IMessage<Integer, String> msg = String :: valueOf;
//		String str = msg.zhuanhuan(1000);
//		System.out.println(str.replaceAll("0", "9"));
		
//		// 使用字符串的toUpperCase方法來做upper的實現方法
//		IMessage2<String> msg = "hello" :: toUpperCase;
//		String upper = msg.upper();
//		
//		System.out.println(upper);
		
		
//		IMessage3<String> msg = String :: compareTo;
//		int compare = msg.compare("A", "B");
//		
//		System.out.println(compare);
		
		
		IMessage4<Book> msg = Book :: new;
		Book book = msg.create("jdk8自學成才", 18.0);
		System.out.println(book);
		
		
	}

}


class Book {
	private String title;
	private double price;
	
	public Book(String title, double price) {
		this.title = title;
		this.price = price;
	}
	
	@Override
	public String toString() {
		return "書名:" + this.title + " 價格:" + this.price;
	}
}



@FunctionalInterface
interface IMessage4<C> {
	public C create(String  t, double p);
}

interface IMessage<P, R> {
	public R zhuanhuan(P p);
}

@FunctionalInterface  // 表示這個接口是函數式接口,只能定義一個方法
interface IMessage2<R> {
	public R upper();
}

@FunctionalInterface
interface IMessage3<P> {
	public int compare(P p1, P p2);
}

 

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