Java大学汽车商店题

img


前三个代码如下
1.Vehicles
public class Vehicles {
public String brand;
public String color;

void showInfo() {
    System.out.println("商标:" + brand + "颜色:" + color);
}

public Vehicles(String brand, String color) {
    this.brand = brand;
    this.color = color;
}

public String brand() {
    return brand;
}

public String color() {
    return color;
}

}
2.Car
public class Car extends Vehicles {

private int seats;

public Car(String brand, String color, int seat) {
    super(brand,color );
    this.seats = seat;
}

public void showCar() {
    System.out.println("车辆类型:小汽车");
    showInfo();
    System.out.println("车辆座位数:" + this.seats);
}

}
3.Truck
public class Truck extends Vehicles {
public double load;

public Truck(String brand, String color, double load) {
    super(brand, color);
    this.load = load;
}

void showTruck() {
    System.out.println("车辆类型:卡车");
    showInfo();
    System.out.println("载重:" + load);
}

}
第四个VehicleShop和第五个VehicleShop咋写
求指点,感谢!

public class VehicleShop {
    int total;
    Vehicles[] v;
    public VehicleShop(int n) {
        this.v = new Vehicles[n>0?n:1];
        this.total = 0;
    }
    void add(Vehicles vehicle) {
        int n = total + 1;
        if(v.length<n) {
            Vehicles[] t = new Vehicles[2*v.length];
            for(int i=0; i<v.length; i++) {
                t[i]=v[i];
            }
            v = t;
        }
        v[n-1] = vehicle;
        total = n;
    }
    void show() {
        for(int i=0; i<total; i++) {
            v[i].showInfo();
        }
    }
    public static void main(String[] args) {
        VehicleShop shop = new VehicleShop(10);
        shop.add(new Car("宝马", "红色", 11));
        shop.show();
    }
}

输出

商标:宝马颜色:红色