2013-03-31 81 views

回答

2

教程Generics爲您提供了所需的所有信息。部分Generic types顯示瞭如何聲明和實現自定義類。

class MyClass<T> { 
    ... 
} 
3

你可以把任何級別的<>,只要它滿足了泛型類的限制:

ArrayList<MyClass> arrayList = new ArrayList<MyClass>(); 

您可以定義自己的類使用<>簡單:

class MyGenericClass<E> { 
    E e; 
    MyGenericClass(E e) { this.e = e; } 

    E getE() { return e; } 
    void setE(E e) { this.e = e; } 
} 

現在你可以讓你自己:

MyGenericClass<String> stuff = new MyGenericClass<String>("Foo"); 
System.out.println(stuff.getE()); 
相關問題