我學習泛型的Java,因爲我想實現這一點:
我有2個班使用1個普通類,並且在這個普通類中我想創建一個泛型類的對象。這裏有一段我的代碼,所以它會是最簡單的。
//MyFirstClass.java
class MyFirstClass{
...
MyExpandableListAdapter<FirstFilter> mAdapter = new MyExpandableListAdapter<FirstFilter>();
...
}
//MySecondClass.java
class MySecondClass{
...
MyExpandableListAdapter<SecondFilter> mAdapter = new MyExpandableListAdapter<SecondFilter>();
...
}
//common class MyExpandableListAdapter.java
public class MyExpandableListAdapter<E extends Filter> extends BaseExpandableListAdapter implements Filterable {
private E filter;
...
public Filter getFilter(){
if (filter== null)
filter = new <E>(); // Here I want to create an Object, but I get an error on <E>
return filter;
}
}
是否可以做到這一點?我怎麼能做到這一點? 請幫幫我。非常感謝你。
非常感謝你,我已經解決了你的幫助:)祝你有美好的一天;) – sleone08 2012-02-27 09:39:34