用java实现下面这几个方法,只需要作打印即可:加速:车辆可以加速来增加速度。 刹车:车辆可以使用刹车来减速或停车。 转向:车辆可以转向来改变行驶方向。 熄火:车辆可以熄火来停止发动机运转。
时间: 2024-05-22 11:16:18 浏览: 91
接口的引用示例-java思想介绍PPT
public class Car {
private int speed;
private boolean engineOn;
private String direction;
public Car() {
speed = 0;
engineOn = false;
direction = "straight";
}
public void accelerate() {
if (engineOn) {
speed += 10;
System.out.println("Accelerating, speed is now " + speed);
} else {
System.out.println("Cannot accelerate, engine is off.");
}
}
public void brake() {
if (speed > 0) {
speed -= 10;
System.out.println("Braking, speed is now " + speed);
} else {
System.out.println("Already stopped.");
}
}
public void turn(String newDirection) {
direction = newDirection;
System.out.println("Turning " + direction);
}
public void turnOffEngine() {
engineOn = false;
System.out.println("Engine off.");
}
}
阅读全文