为了账号安全,请及时绑定邮箱和手机立即绑定

最多可存储 100 个的方法

最多可存储 100 个的方法

九州编程 2021-11-24 15:57:54
我有点不清楚如何正确地制作这个类。我正在制作的数组找不到符号,我不确定如何更正。编译错误是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


查看完整回答
反对 回复 2021-11-24
  • 1 回答
  • 0 关注
  • 126 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信