|
|
package com.automobile.parts;
|
|
|
|
|
|
/**
|
|
|
* 变速箱类 - 与汽车是组合关系
|
|
|
* 变速箱与引擎和车轮之间也存在关联关系
|
|
|
*/
|
|
|
public class Transmission {
|
|
|
private String type;
|
|
|
private int gearCount;
|
|
|
private int currentGear;
|
|
|
private boolean automatic;
|
|
|
|
|
|
/**
|
|
|
* 构造方法 - 创建变速箱时设置默认属性
|
|
|
*/
|
|
|
public Transmission() {
|
|
|
this.type = "自动变速箱";
|
|
|
this.gearCount = 6;
|
|
|
this.currentGear = 0; // 空挡
|
|
|
this.automatic = true;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据速度换挡
|
|
|
* @param speed 当前速度(km/h)
|
|
|
*/
|
|
|
public void shiftGears(int speed) {
|
|
|
int newGear;
|
|
|
|
|
|
if (speed == 0) {
|
|
|
newGear = 0; // 空挡
|
|
|
} else if (speed <= 20) {
|
|
|
newGear = 1;
|
|
|
} else if (speed <= 40) {
|
|
|
newGear = 2;
|
|
|
} else if (speed <= 60) {
|
|
|
newGear = 3;
|
|
|
} else if (speed <= 80) {
|
|
|
newGear = 4;
|
|
|
} else if (speed <= 120) {
|
|
|
newGear = 5;
|
|
|
} else {
|
|
|
newGear = 6;
|
|
|
}
|
|
|
|
|
|
if (newGear != currentGear) {
|
|
|
this.currentGear = newGear;
|
|
|
System.out.println("变速箱换挡到 " + getGearName(currentGear));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取档位名称
|
|
|
* @param gear 档位号
|
|
|
* @return 档位名称
|
|
|
*/
|
|
|
private String getGearName(int gear) {
|
|
|
switch (gear) {
|
|
|
case 0: return "空挡(N)";
|
|
|
case 1: return "1档";
|
|
|
case 2: return "2档";
|
|
|
case 3: return "3档";
|
|
|
case 4: return "4档";
|
|
|
case 5: return "5档";
|
|
|
case 6: return "6档";
|
|
|
default: return "未知档位";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 手动换挡(模拟手动模式)
|
|
|
* @param gear 目标档位
|
|
|
*/
|
|
|
public void manualShift(int gear) {
|
|
|
if (gear >= 0 && gear <= gearCount) {
|
|
|
this.currentGear = gear;
|
|
|
System.out.println("手动换挡到 " + getGearName(gear));
|
|
|
} else {
|
|
|
System.out.println("无效的档位选择");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 切换到倒挡
|
|
|
*/
|
|
|
public void reverse() {
|
|
|
this.currentGear = -1;
|
|
|
System.out.println("切换到倒挡(R)");
|
|
|
}
|
|
|
|
|
|
// Getter 和 Setter 方法
|
|
|
public String getType() {
|
|
|
return type;
|
|
|
}
|
|
|
|
|
|
public void setType(String type) {
|
|
|
this.type = type;
|
|
|
}
|
|
|
|
|
|
public int getGearCount() {
|
|
|
return gearCount;
|
|
|
}
|
|
|
|
|
|
public void setGearCount(int gearCount) {
|
|
|
this.gearCount = gearCount;
|
|
|
}
|
|
|
|
|
|
public int getCurrentGear() {
|
|
|
return currentGear;
|
|
|
}
|
|
|
|
|
|
public boolean isAutomatic() {
|
|
|
return automatic;
|
|
|
}
|
|
|
|
|
|
public void setAutomatic(boolean automatic) {
|
|
|
this.automatic = automatic;
|
|
|
}
|
|
|
} |