求java大神啊
代码如下:
class Vehicle {
private String name;
private int size;
private int weight;
private int speed;
public Vehicle(String name, int size, int weight) {
this(name, size, weight, 0);
}
public Vehicle(String name, int size, int weight, int speed) {
this.name = name;
this.size = size;
this.weight = weight;
this.speed = speed;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public void speedUp(int speed) {
this.speed += speed;
}
public void speedDown(int speed) {
this.speed -= speed;
if (this.speed < 0) {
this.speed = 0;
}
}
}
public class App {
public static void main(String[] args) {
Vehicle vehicle = new Vehicle("Car", 10, 1000, 0);
System.out.println("Name = " + vehicle.getName() + ", Size = " + vehicle.getSize() + ", Weight = " + vehicle.getWeight() + ", Speed = " + vehicle.getSpeed());
vehicle.speedUp(50);
System.out.println("Name = " + vehicle.getName() + ", Size = " + vehicle.getSize() + ", Weight = " + vehicle.getWeight() + ", Speed = " + vehicle.getSpeed());
vehicle.speedUp(50);
System.out.println("Name = " + vehicle.getName() + ", Size = " + vehicle.getSize() + ", Weight = " + vehicle.getWeight() + ", Speed = " + vehicle.getSpeed());
vehicle.speedDown(20);
System.out.println("Name = " + vehicle.getName() + ", Size = " + vehicle.getSize() + ", Weight = " + vehicle.getWeight() + ", Speed = " + vehicle.getSpeed());
}
}
运行结果: