Мне немного непонятно, как правильно сделать этот урок.Массив, который я создаю, не может найти символ, и я не уверен, как это исправить.
Ошибка при компиляции 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();
}
}