亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

最多可存儲 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 關注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號