设计一个交通工具类Vehicle
时间: 2024-10-21 15:01:35 浏览: 53
设计一个交通工具类`Vehicle`,通常会在面向对象编程中作为基础类,用于表示所有可以移动的实体。这个类可能会包含一些通用属性,如:
```java
public class Vehicle {
// 属性
private String modelName;
private int maxSpeed;
private boolean isMoving; // 表示车辆是否正在行驶
private double fuelCapacity;
private double currentFuelLevel;
// 构造函数
public Vehicle(String modelName, int maxSpeed) {
this.modelName = modelName;
this.maxSpeed = maxSpeed;
this.isMoving = false;
this.fuelCapacity = 0.0; // 新车默认油箱为空
this.currentFuelLevel = 0.0;
}
// 方法
public void start() {
if (!isMoving && currentFuelLevel > 0) {
isMoving = true;
System.out.println(modelName + "已启动.");
} else {
System.out.println("车辆无法启动,可能是燃料不足或已启动状态.");
}
}
public void stop() {
if (isMoving) {
isMoving = false;
System.out.println(modelName + "已停止.");
}
}
public void refuel(double fuelAmount) {
if (fuelAmount >= 0) {
currentFuelLevel += fuelAmount;
if (currentFuelLevel > fuelCapacity) {
currentFuelLevel = fuelCapacity;
}
System.out.println(modelName + "加满燃料到:" + currentFuelLevel + "升");
} else {
System.out.println("无效的加油量.");
}
}
public double getCurrentFuelLevel() {
return currentFuelLevel;
}
// 其他可能的方法,比如检查燃油状态等
//
阅读全文