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.
95 lines
2.3 KiB
95 lines
2.3 KiB
package com.automobile.parts;
|
|
|
|
/**
|
|
* 活塞类 - 与引擎是组合关系
|
|
* 活塞是引擎的必要组成部分,不能独立于引擎存在
|
|
* 展示了多层级组合关系的实现
|
|
*/
|
|
public class Piston {
|
|
private int id; // 活塞编号
|
|
private double diameter; // 活塞直径
|
|
private double stroke; // 活塞行程
|
|
private boolean isMoving; // 活塞是否在运动
|
|
private int position; // 活塞当前位置
|
|
|
|
/**
|
|
* 构造方法 - 创建活塞时设置基本属性
|
|
* @param id 活塞编号
|
|
*/
|
|
public Piston(int id) {
|
|
this.id = id;
|
|
this.diameter = 86.0; // 毫米
|
|
this.stroke = 86.0; // 毫米
|
|
this.isMoving = false;
|
|
this.position = 0; // 初始位置
|
|
}
|
|
|
|
/**
|
|
* 活塞开始运动
|
|
*/
|
|
public void move() {
|
|
this.isMoving = true;
|
|
System.out.println("活塞 " + id + " 开始运动");
|
|
}
|
|
|
|
/**
|
|
* 活塞停止运动
|
|
*/
|
|
public void stop() {
|
|
this.isMoving = false;
|
|
System.out.println("活塞 " + id + " 停止运动");
|
|
}
|
|
|
|
/**
|
|
* 活塞加速运动
|
|
* @param rpm 引擎转速,影响活塞运动速度
|
|
*/
|
|
public void moveFaster(int rpm) {
|
|
if (isMoving) {
|
|
this.position = rpm % 360; // 模拟活塞位置变化
|
|
System.out.println("活塞 " + id + " 加速到 " + rpm + " RPM, 当前位置: " + position + "度");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取活塞工作状态
|
|
* @return 活塞工作状态描述
|
|
*/
|
|
public String getStatus() {
|
|
return "活塞 " + id + ": " + (isMoving ? "运动中" : "静止") +
|
|
", 位置: " + position + "度";
|
|
}
|
|
|
|
// Getter 和 Setter 方法
|
|
public int getId() {
|
|
return id;
|
|
}
|
|
|
|
public void setId(int id) {
|
|
this.id = id;
|
|
}
|
|
|
|
public double getDiameter() {
|
|
return diameter;
|
|
}
|
|
|
|
public void setDiameter(double diameter) {
|
|
this.diameter = diameter;
|
|
}
|
|
|
|
public double getStroke() {
|
|
return stroke;
|
|
}
|
|
|
|
public void setStroke(double stroke) {
|
|
this.stroke = stroke;
|
|
}
|
|
|
|
public boolean isMoving() {
|
|
return isMoving;
|
|
}
|
|
|
|
public int getPosition() {
|
|
return position;
|
|
}
|
|
} |