我有點不清楚如何正確地制作這個類。我正在制作的數組找不到符號,我不確定如何更正。編譯錯誤是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
提交
取消