我有一个名为Vehicle的课程。它有四个子类:汽车,摩托车,货车和皮卡。 以下是各自的声明:
//vehicle
String licenseNo = null;
int engineCap = 0;
int cc = 0;
String brand = null;
String model = null;
int passengers = 0;
double price = 0;
//car
String material = null;
String paintType = null;
String colorC = null;
//motorcycle
String mainColor = null;
String seatColor = null;
String seatMaterial = null;
//van
int luggageCap = 0;
String colorV = null;
//pickup
int tonsCap = 0;
int yearsUsed = 0;
String tyreQuality = null;
注意:这不是我在实际课程中宣称它们的方式。我是如何在我试图写的方法中初始化它们的。
以下是该方法的其余部分:
FileReader fileReader = new FileReader(filename);
BufferedReader in = new BufferedReader(fileReader);
int count = 0;
Scanner scan = new Scanner(in);
while (scan.hasNext())
{
vehicledata[count++] = new Vehicle(scan.next(), scan.nextInt(), scan.nextInt(), scan.next(), scan.next() ,scan.nextInt(), scan.nextDouble());
}
我有一个数组:
static Vehicle vehicledata[] = new Vehicle[50];
保存文件中的值。该文件看起来像这样: Text file im reading from
我可以读取普通Vehicle类中的值。但我需要分离子类,以便我可以按类型对它们进行排序。 (如:汽车列表,面包车列表)
我在:
vehicledata[count++] = new Vehicle(scan.next(), scan.nextInt(), scan.nextInt(), scan.next(), scan.next() ,scan.nextInt(), scan.nextDouble());
部分..而且我不知道如何在这一点上区分汽车和其他东西。 (从车辆中最后一个属性之后的文件中,将会有车辆类型,后面跟着该子类的属性)
关于我如何做到这一点的任何线索?一个if条件?但我怎样才能在
中实现它vehicledata[count++] = new Vehicle(scan.next(), scan.nextInt(), scan.nextInt(), scan.next(), scan.next() ,scan.nextInt(), scan.nextDouble());`
答案 0 :(得分:1)
您可以将所有内容放在csv文件中,例如:
L1412,10,100,日产,轿车
并创建一个静态工厂方法来创建所需的实例:
public static Vehical createVehical(String line){
String[] parts = line.split(",");
String type = parts[parts.length -1]; //get the last column
switch (type) {
case "Van":
return new Car(a, b, c,...);
case "Sedan":
return new Van(a, b, c...);
default:
return null;
}
}
对于未来检查子类,请使用
if(vehical instanceof Sedan){
}else if(vehical instanceof Van){
}else...
或者您可以查看Java Generic Collections,它可以帮助您解决问题,希望它有所帮助
答案 1 :(得分:0)
你有几个继承自车辆的类
在您的文件中,您必须描述对象的类型,然后描述所有属性,例如:
van
xxxx //licenseNo
xxxx //engineCap
xxxx //cc
xxxx //brand
xxxx //model
xxxx //passengers
xxxx //price
xxxx //luggageCap
xxxx //colorV
现在你必须声明FileReader,BufferedReader和Scanner ...
好吧!
现在你有几种方法可以读取文件并存储在最简单的数组中:
Van[] arrVan = new Van[50];
Car[] arrCar = new Car[50];
//etc
while (scan.hasNext())
{
String str = scan.next();
if(str == "van")
{
Van van = new Van();
van.setlicenseNo(scan.next());
van.setEngineCap(scan.next());
//etc
// add van to arrVan
}
else if(str == "car")
{
Car car = new Car();
//etc
//add car to arrCar
}
//etc
}
答案 2 :(得分:0)
谢谢你的努力:) 通过简单地更改文件的格式来管理解决我遇到的问题
while (scan.hasNext())
{
if(scan.next().toLowerCase() == "car")
{
carsdata[count++] = new Car(scan.next(), scan.nextInt(), scan.nextInt(), scan.next(), scan.next() ,scan.nextInt(), scan.nextDouble(), scan.next(), scan.next(), scan.next());
}
else if(scan.next().toLowerCase() == "motorcycle")
{
motorcyclesdata[count++] = new Motorcycle(scan.next(), scan.nextInt(), scan.nextInt(), scan.next(), scan.next() ,scan.nextInt(), scan.nextDouble(), scan.next(), scan.next(), scan.next());
}
else if(scan.next().toLowerCase() == "van")
{
vansdata[count++] = new Van(scan.next(), scan.nextInt(), scan.nextInt(), scan.next(), scan.next() ,scan.nextInt(), scan.nextDouble(), scan.nextInt(), scan.next());
}
else if(scan.next().toLowerCase() == "pickup")
{
pickupsdata[count++] = new Pickup(scan.next(), scan.nextInt(), scan.nextInt(), scan.next(), scan.next() ,scan.nextInt(), scan.nextDouble(), scan.nextInt(), scan.nextInt(), scan.next());
}
}