# 自訂泛型 ###### tags: 'Ming' {%hackmd BJrTq20hE %} import java.util.*; //要給其他人使用(用途不一),所以允許寫任意識別字表示一個暫時的型別 class MyGenericType<Type> { private List<Type> list; public MyGenericType() { list = new Vector<Type>(); } public void add(Type t) { list.add(t); } public Type get(int i) { return list.get(i); } } //(當他人)宣告使用時,才真正設定(決定)是什麼樣的型別,方法內容將發生轉換 public class MyGeneric { public static void main(String[] args) { MyGenericType<String> myGeneric = new MyGenericType<String>(); for (int i = 0; i < 3; i++) { myGeneric.add("number" + i); System.out.println(myGeneric.get(i)); } } }