Java汽车继承类的设计方案

问题描述

我们现在要设计一个汽车销售系统,该系统需要处理不同类型的汽车,包括轿车、越野车和货车。每种汽车都有共同的属性和行为,但也有一些特定的属性和行为。我们需要使用继承来实现这个系统,并且能够方便地添加新的汽车类型。

方案设计

1. 定义父类Car

我们首先创建一个Car类作为所有汽车的父类,该类包含一些汽车的共同属性和行为。

public class Car {
    private String brand;
    private String model;
    private double price;

    public Car(String brand, String model, double price) {
        this.brand = brand;
        this.model = model;
        this.price = price;
    }

    public String getBrand() {
        return brand;
    }

    public String getModel() {
        return model;
    }

    public double getPrice() {
        return price;
    }

    public void displayInfo() {
        System.out.println("Brand: " + brand);
        System.out.println("Model: " + model);
        System.out.println("Price: $" + price);
    }
}

2. 创建子类继承Car类

接下来,我们为每种具体的汽车类型创建一个子类,分别是Sedan(轿车)、SUV(越野车)和Truck(货车)。这些子类继承了父类Car的属性和方法,并且可以添加自己特定的属性和方法。

a. 子类Sedan
public class Sedan extends Car {
    private int numSeats;

    public Sedan(String brand, String model, double price, int numSeats) {
        super(brand, model, price);
        this.numSeats = numSeats;
    }

    public int getNumSeats() {
        return numSeats;
    }

    @Override
    public void displayInfo() {
        super.displayInfo();
        System.out.println("Number of Seats: " + numSeats);
    }
}
b. 子类SUV
public class SUV extends Car {
    private boolean offRoadCapable;

    public SUV(String brand, String model, double price, boolean offRoadCapable) {
        super(brand, model, price);
        this.offRoadCapable = offRoadCapable;
    }

    public boolean isOffRoadCapable() {
        return offRoadCapable;
    }

    @Override
    public void displayInfo() {
        super.displayInfo();
        System.out.println("Off-Road Capable: " + offRoadCapable);
    }
}
c. 子类Truck
public class Truck extends Car {
    private double cargoCapacity;

    public Truck(String brand, String model, double price, double cargoCapacity) {
        super(brand, model, price);
        this.cargoCapacity = cargoCapacity;
    }

    public double getCargoCapacity() {
        return cargoCapacity;
    }

    @Override
    public void displayInfo() {
        super.displayInfo();
        System.out.println("Cargo Capacity: " + cargoCapacity + " tons");
    }
}

3. 使用汽车继承类

a. 创建汽车对象

我们可以使用这些汽车继承类来创建不同类型的汽车对象,并使用它们的方法和属性。

public class CarSalesSystem {
    public static void main(String[] args) {
        Car sedan = new Sedan("Toyota", "Camry", 25000.0, 5);
        Car suv = new SUV("Jeep", "Wrangler", 35000.0, true);
        Car truck = new Truck("Ford", "F-150", 40000.0, 1.5);

        sedan.displayInfo();
        System.out.println();

        suv.displayInfo();
        System.out.println();

        truck.displayInfo();
    }
}

输出结果:

Brand: Toyota
Model: Camry
Price: $25000.0
Number of Seats: 5

Brand: Jeep
Model: Wrangler
Price: $35000.0
Off-Road Capable: true

Brand: Ford
Model: F-150
Price: $40000.0
Cargo Capacity: 1.5 tons
b. 类图

下面是使用mermaid语法表示的汽车继承类的类图。

classDiagram
    class Car {
        -String brand
        -String model
        -double price
        +Car(brand: String, model: String, price: double)
        +getBrand(): String
        +getModel(): String
        +getPrice(): double
        +displayInfo(): void