-3
我有四個類似的類,它們只在內部創建的對象類型上有所不同。
像這樣創建一個通用(通用?)類我java
public class ImportingWarehouseP //ImportWarehouseW, ImportDictionary, ImportSupplier
{
private ArrayList<WarehouseP> list = new ArrayList<WarehouseP>();
public void importWarehouseP(File fileName) throws FileNotFoundException
{
FileReader in = new FileReader(fileName);
Scanner src = new Scanner(in);
src.useDelimiter("\n");
src.next();
for (int g = 0; src.hasNext(); g++)
{
String record = src.next();
String [] asdf = record.trim().split(";|:");
WarehouseP ob = new WarehouseP (asdf); //here is the difference instead WarehouseP can be WarehouseW, Dictionary, Supplier
list.add(ob);
}
}
public ArrayList<WarehouseP> getList()
{
return list;
}
}
是否有可能創建一個uniwersal類「導入」,只有在主指定我想要什麼類型的列表中得到什麼? e.g
public static void main(String[] args){
File warehousePFile = new File(path);
ImportWarehouseP ImpWP = new Import<WarehouseP>();
ImpWP.importWarehouseP(warehousePFile);
ArrayList<WarehouseP> recordsWP = ImpWP.getList();
}
您正在描述泛型。 http://docs.oracle.com/javase/tutorial/java/generics/ –
@JeroenVannevel不,他不是,因爲泛型不能創建對象。 –
啊,你說得對@TimB。忘記Java不能實例化一個泛型類型。 –