You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
3.1 KiB
130 lines
3.1 KiB
package com.automobile.parts;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* 引擎类 - 与汽车是组合关系
|
|
* 引擎包含活塞等部件,展示更深层级的组合关系
|
|
*/
|
|
public class Engine {
|
|
private String type;
|
|
private int cylinders;
|
|
private int horsepower;
|
|
private int currentRPM;
|
|
private boolean isRunning;
|
|
|
|
// 组合关系 - 引擎包含多个活塞
|
|
private List<Piston> pistons;
|
|
|
|
/**
|
|
* 构造方法 - 创建引擎时同时创建活塞
|
|
* 体现了多层级组合关系
|
|
*/
|
|
public Engine() {
|
|
this.type = "四缸涡轮增压";
|
|
this.cylinders = 4;
|
|
this.horsepower = 200;
|
|
this.currentRPM = 0;
|
|
this.isRunning = false;
|
|
|
|
// 创建活塞(组合关系)
|
|
this.pistons = new ArrayList<>();
|
|
for (int i = 0; i < cylinders; i++) {
|
|
this.pistons.add(new Piston(i + 1));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 启动引擎
|
|
*/
|
|
public void start() {
|
|
if (!isRunning) {
|
|
isRunning = true;
|
|
currentRPM = 800; // 怠速转速
|
|
System.out.println("引擎启动成功,当前转速: " + currentRPM + " RPM");
|
|
// 启动所有活塞
|
|
for (Piston piston : pistons) {
|
|
piston.move();
|
|
}
|
|
} else {
|
|
System.out.println("引擎已经在运行中");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 停止引擎
|
|
*/
|
|
public void stop() {
|
|
if (isRunning) {
|
|
isRunning = false;
|
|
currentRPM = 0;
|
|
System.out.println("引擎已停止");
|
|
// 停止所有活塞
|
|
for (Piston piston : pistons) {
|
|
piston.stop();
|
|
}
|
|
} else {
|
|
System.out.println("引擎已经停止");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 增加转速
|
|
* @param rpm 目标转速
|
|
*/
|
|
public void increaseRPM(int rpm) {
|
|
if (isRunning) {
|
|
currentRPM = rpm;
|
|
System.out.println("引擎转速提升到: " + currentRPM + " RPM");
|
|
// 所有活塞加速运动
|
|
for (Piston piston : pistons) {
|
|
piston.moveFaster(rpm);
|
|
}
|
|
} else {
|
|
System.out.println("请先启动引擎");
|
|
}
|
|
}
|
|
|
|
// Getter 和 Setter 方法
|
|
public String getType() {
|
|
return type;
|
|
}
|
|
|
|
public void setType(String type) {
|
|
this.type = type;
|
|
}
|
|
|
|
public int getCylinders() {
|
|
return cylinders;
|
|
}
|
|
|
|
public void setCylinders(int cylinders) {
|
|
this.cylinders = cylinders;
|
|
// 重新创建对应数量的活塞
|
|
this.pistons = new ArrayList<>();
|
|
for (int i = 0; i < cylinders; i++) {
|
|
this.pistons.add(new Piston(i + 1));
|
|
}
|
|
}
|
|
|
|
public int getHorsepower() {
|
|
return horsepower;
|
|
}
|
|
|
|
public void setHorsepower(int horsepower) {
|
|
this.horsepower = horsepower;
|
|
}
|
|
|
|
public int getCurrentRPM() {
|
|
return currentRPM;
|
|
}
|
|
|
|
public boolean isRunning() {
|
|
return isRunning;
|
|
}
|
|
|
|
public List<Piston> getPistons() {
|
|
return pistons;
|
|
}
|
|
} |