package com.automobile.model; import com.automobile.parts.SteeringWheel; import com.automobile.parts.Seat; import com.automobile.parts.Engine; import com.automobile.parts.Transmission; import com.automobile.parts.Wheel; import com.automobile.system.ElectronicSystem; import java.util.ArrayList; import java.util.List; /** * 汽车类 - 展示组合关系的主类 * 汽车包含方向盘、座椅、引擎、变速箱、车轮和电子系统等部件 * 这些部件与汽车是组合关系,当汽车被销毁时,这些部件也应该被销毁 */ public class Car { private String brand; private String model; private String vin; // 组合关系 - 方向盘是汽车的一部分 private SteeringWheel steeringWheel; // 组合关系 - 座椅是汽车的一部分 private List seats; // 组合关系 - 引擎是汽车的一部分 private Engine engine; // 组合关系 - 变速箱是汽车的一部分 private Transmission transmission; // 组合关系 - 车轮是汽车的一部分 private List wheels; // 组合关系 - 电子系统是汽车的一部分 private ElectronicSystem electronicSystem; /** * 构造方法 - 创建汽车时同时创建其所有必要部件 * 体现了组合关系的特点:整体负责创建和管理部分的生命周期 */ public Car(String brand, String model, String vin) { this.brand = brand; this.model = model; this.vin = vin; // 创建方向盘(组合关系) this.steeringWheel = new SteeringWheel(); // 创建座椅(组合关系) this.seats = new ArrayList<>(); this.seats.add(new Seat(Seat.SeatType.DRIVER)); // 驾驶员座椅 this.seats.add(new Seat(Seat.SeatType.PASSENGER)); // 乘客座椅 this.seats.add(new Seat(Seat.SeatType.REAR)); // 后排座椅 this.seats.add(new Seat(Seat.SeatType.REAR)); // 后排座椅 // 创建引擎(组合关系) this.engine = new Engine(); // 创建变速箱(组合关系) this.transmission = new Transmission(); // 创建车轮(组合关系) this.wheels = new ArrayList<>(); for (int i = 0; i < 4; i++) { this.wheels.add(new Wheel()); } // 创建电子系统(组合关系) this.electronicSystem = new ElectronicSystem(); } /** * 启动汽车 */ public void start() { System.out.println(brand + " " + model + " 启动中..."); engine.start(); electronicSystem.initializate(); System.out.println("汽车启动成功!"); } /** * 停止汽车 */ public void stop() { System.out.println(brand + " " + model + " 停止中..."); engine.stop(); electronicSystem.shutdown(); System.out.println("汽车已停止!"); } /** * 加速汽车 */ public void accelerate(int speed) { System.out.println("加速到 " + speed + " km/h"); engine.increaseRPM(speed * 100); transmission.shiftGears(speed); } /** * 显示汽车信息 */ public void displayInfo() { System.out.println("===== 汽车信息 ====="); System.out.println("品牌: " + brand); System.out.println("型号: " + model); System.out.println("VIN码: " + vin); System.out.println("方向盘类型: " + steeringWheel.getType()); System.out.println("座椅数量: " + seats.size()); System.out.println("引擎类型: " + engine.getType()); System.out.println("变速箱类型: " + transmission.getType()); System.out.println("车轮数量: " + wheels.size()); System.out.println("电子系统状态: " + electronicSystem.getStatus()); System.out.println("==================="); } // Getter 和 Setter 方法 public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } public String getVin() { return vin; } public void setVin(String vin) { this.vin = vin; } public SteeringWheel getSteeringWheel() { return steeringWheel; } public List getSeats() { return seats; } public Engine getEngine() { return engine; } public Transmission getTransmission() { return transmission; } public List getWheels() { return wheels; } public ElectronicSystem getElectronicSystem() { return electronicSystem; } }