我有点不清楚如何正确地制作这个类。我正在制作的数组找不到符号,我不确定如何更正。
编译错误是ArrayList<Vehicle> db = new ArrayList<Vehicle>();我想我只需要在某处初始化它才能正常工作。
感谢您的帮助。
class Vehicle {
int capacity;
String make;
void setCapacity(int setCapacity) {
this.capacity = setCapacity;
System.out.println("New Capacity = " + setCapacity);
}
Vehicle(int theCapacity, String theMake) {
capacity = theCapacity;
make = theMake;
}
void print() {
System.out.println("Vehicle Info:");
System.out.println(" capacity = " + capacity + "cc" );
System.out.println(" make = " + make );
}
}
class Car extends Vehicle {
public String type;
public String model;
public Car(int theCapacity, String theMake, String theType, String theModel) {
super(theCapacity, theMake);
type = theType;
model = theModel;
}
@Override
public void print() {
super.print();
System.out.println(" type = " + type);
System.out.println(" model = " + model);
}
@Override
public void setCapacity(int setCapacity) {
System.out.println("Cannot change capacity of a car");
}
}
class VehicleDB {
ArrayList<Vehicle> db = new ArrayList<Vehicle>();
void addVehicle(Vehicle c){
db.add(c);
}
void print(){
System.out.println("=== Vehicle Data Base ===");
for(Vehicle v: db){
v.print();
}
}
}
class Task4 {
public static void main (String[]args) {
VehicleDB db = new VehicleDB () ;
db.addVehicle (new Car (1200,"Holden","sedan","Barina"));
db.addVehicle(new Vehicle(1500,"Mazda"));
db.print();
}
跃然一笑
相关分类