我有点不清楚如何正确地制作这个类。我正在制作的数组找不到符号,我不确定如何更正。编译错误是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(); }
1 回答
跃然一笑
TA贡献1826条经验 获得超6个赞
只有您必须import java.util.ArrayList;在文件顶部添加@ Robby Cornelissen Said。
导入 java.util.ArrayList;
public class Vehicle {
// your Code
}
添加后它工作正常广告给出输出:
=== Vehicle Data Base ===
Vehicle Info:
capacity = 1200cc
make = Holden
type = sedan
model = Barina
Vehicle Info:
capacity = 1500cc
make = Mazda
添加回答
举报
0/150
提交
取消