希望下面的 mList 可以不 cast 直接使用,请问是不是做不到?
import java.util.LinkedList;
import java.util.List;
public class AbsList {
private List<?> mList;
private String mType;
public AbsList newInstance(String type) {
mType = type;
switch (type) {
case "String":
mList = new LinkedList<String>();
break;
case "Number":
mList = new LinkedList<Number>();
}
return this;
}
public void go() {
switch (mType) {
case "String":
((List<String>) mList).add("A");
for (String s : (List<String>) mList)
System.out.println(s);
break;
case "Number":
((List<Number>) mList).add(0);
for (Number n : (List<Number>) mList)
System.out.println("" + n);
}
}
public static void main (String[] args) {
new AbsList().newInstance("String").go();
}
}