1. 程式人生 > >Java泛型---泛型的巢狀

Java泛型---泛型的巢狀

泛型巢狀的使用 

    建立一個PO類

/**
 * 測試泛型的巢狀
 * @author Administrator
 *
 * @param <T>
 */
public class Class01<T> {

	private T stu;

	public T getStu() {
		return stu;
	}

	public void setStu(T stu) {
		this.stu = stu;
	}
	
}

    泛型的巢狀使用及HashMap中的典型泛型巢狀使用方法

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import java.util.Map.Entry;;

/**
 * 泛型的巢狀 --->由外向內拆分
 * @author Administrator
 *
 */
public class Generics {
	public static void main(String[] args) {
		Student01<String> stu = new Student01<String>();
		
		stu.setScore("AA");
		System.out.println(stu.getScore());
		
		//巢狀 --->多層包裹
		Class01<Student01<String>> c1 = new Class01<Student01<String>>();
		c1.setStu(stu);
		stu = c1.getStu();
		String score = stu.getScore();
		System.out.println(score);
		
		//HashMap中的泛型巢狀
		
		Map<String, String> map = new HashMap<String, String>();
		
		map.put("01", "AA");
		map.put("02", "BB");
		
		Set<Entry<String,String>> entrySet = map.entrySet();
		//增強的for迴圈,遍歷entrySet
		for (Entry<String, String> entry : entrySet) {
			System.out.println(entry);
			String key = entry.getKey();
			String value = entry.getValue();
			System.out.println(key+"--->"+value);
		}
	}
}