-2
例如看看這個:什麼在類名末尾的角度括號的符號是什麼意思?
ArrayList<String> arrayList = new ArrayList<String>();
我可以使用<> s的自定義類?我如何使用它們?
例如看看這個:什麼在類名末尾的角度括號的符號是什麼意思?
ArrayList<String> arrayList = new ArrayList<String>();
我可以使用<> s的自定義類?我如何使用它們?
教程Generics爲您提供了所需的所有信息。部分Generic types顯示瞭如何聲明和實現自定義類。
class MyClass<T> {
...
}
你可以把任何級別的<>
,只要它滿足了泛型類的限制:
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());
的ArrayList S =新的ArrayList ()? –
Barney
你可以使用這樣的'ArrayList的S =新的ArrayList();',並添加任何類的對象,你要 – Farnabaz
你明白我的問題嗎?像這樣:MyClass class; –
user2223779