我学习泛型的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