最多可存储 100 个的方法

我有点不清楚如何正确地制作这个类。我正在制作的数组找不到符号,我不确定如何更正。


编译错误是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();

   }



九州编程
浏览 127回答 1
1回答

跃然一笑

只有您必须import java.util.ArrayList;在文件顶部添加@ Robby Cornelissen Said。导入 java.util.ArrayList;public class Vehicle {// your Code}添加后它工作正常广告给出输出:=== Vehicle Data Base ===Vehicle Info:&nbsp; capacity = 1200cc&nbsp; make = Holden&nbsp; type = sedan&nbsp; model = BarinaVehicle Info:&nbsp; capacity = 1500cc&nbsp; make = Mazda
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java