package cn.itcast.day2;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
public class GenericTest {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
ArrayList<String> collection2=new ArrayList<String> ();
collection2.add("abc");
ArrayList<Integer> collection3=new ArrayList<Integer> ();
//對於泛型,代碼編譯完就沒有我們定義的泛型啦,也就是說ArrayList<Integer> collection3=new ArrayList<Integer> ();
//編譯完為ArrayList collection3=new ArrayList();
//所以collection2與collection3為同一份位元組碼
//所以泛型是給編譯器看的,運行時已經沒有泛型痕迹
System.out.println(collection2.getClass()==collection3.getClass());
//利用泛型跳過編譯器向泛型對象中傳入其它型別參數
collection3.getClass().getMethod("add", Object.class).invoke(collection3, "abc");
System.out.println(collection3.get(0));
/*
參數化型別與原始類型的相容性:
參數化型別可以引用一個原始類型的對象,編譯報警示告,例如,Collection<String> c = new Vector();//可不可以,不就是編譯器一句話的事嗎?
原始類型可以引用一個參數化型別的對象,編譯報警示告,例如,Collection c = new Vector<String>();//原來的方法接受一個集合參數,新的類型也要能傳進去
*/
printCollection(collection3);
HashMap<String,Integer> maps=new HashMap<String,Integer>();
maps.put("zhangsan", 34);
maps.put("lisi", 35);
maps.put("wangwu", 36);
//獲得key,value集合
Set<Map.Entry<String,Integer>> entrySet=maps.entrySet();
//列印出集合中元素
for(Map.Entry<String,Integer> entry:entrySet){
System.out.println(entry.getKey() + ":::" + entry.getValue());
}
//只有參考型別才能作為泛型方法的實際參數,swap(new int[3],3,5);語句會報告編譯錯誤
swap(new String[]{"abc","xyz","itcast"},1,2);
//swap(new int[]{1,2,3,4,5},2,3);
Object obj="abc";
String x3=autoConvert(obj);
System.out.println(x3);
//通過反射獲得泛型的參數化型別
Method applyMethod=GenericTest.class.getMethod("applyVector", Vector.class);
//獲得參數的泛型型別,可能有多個所以返回一個數組
//Type 是 Java 程式設計語言中所有類型的公用進階介面。
//它們包括原始類型、參數化型別、數群組類型、類型變數和基本類型。
Type[] types=applyMethod.getGenericParameterTypes();
//ParameterizedType為Type子介面,ParameterizedType 表示參數化型別
//此處已知有一個參數化型別,所以直接將第一個賦值給pType
ParameterizedType pType=(ParameterizedType)types[0];
//列印出泛型參數
System.out.println(pType.getActualTypeArguments()[0]);
}
public static void applyVector(Vector<Date> v1){
}
//泛型方法練習--------------------------------
//編寫一個泛型方法,自動將Object類型的對象轉換成其他類型。
public static <T> T autoConvert(Object obj){
return (T)obj;
}
//定義一個方法,可以將任意類型的數組中的所有元素填充為相應類型的某個對象
public static <T> void fillArray(T[] a,T obj){
for(int i=0;i<a.length;i++){
a[i]=obj;
}
}
//泛型方法練習--------------------------------
//只有參考型別才能作為泛型方法的實際參數
public static <T> void swap(T[]a,int i,int j){
T temp=a[i];
a[i]=a[j];
a[j]=temp;
}
//該方法用於列印出任意參數化型別的集合中的所有資料
public static void printCollection(Collection<?> collection){
System.out.println(collection.size());
for(Object obj:collection){
System.out.println(obj);
}
}
}
package cn.itcast.day2;
public class GenericDao<T> {
public void add(T p){
}
public T findById(int Id){
return null;
}
/*public static void update(T obj){
//靜態不能用類上定義的泛型
}*/
//靜態可以使用自己定義的泛型
public static <E> void update(E obj){
}
}
分享一下我的學習資料!!!!
Java全套頂級視頻
http://pro.net.itcast.cn/View-22-1435.aspx
DotNet全套視頻
http://pro.net.itcast.cn/View-23-1435.aspx
PHP全套視頻
http://pro.net.itcast.cn/View-24-1435.aspx
全世界最適合0基礎學習編程的網上校園
http://pro.net.itcast.cn/View-25-1435.aspx
免費申請java全套視頻光碟片地區
http://pro.net.itcast.cn/View-26-1435.aspx