请定义一个交通工具(Vehicle)的类,其中有:
- 属性:速度(speed),体积(size)等
- 方法:移动(move()),设置速度(setSpeed(int speed)),设置体积(setSize(int size))加速speedUp(),减速speedDown()等
在测试类Vehicle中的main()中实例化一个交通工具对象,通过方法给它初始化speed,size的值,并打印出来。另外,调用加速,减速的方法对速度进行改变。
public class Vehicle {
private int speed;
private int size;
Vehicle(int speed,int size) {
this.size = size;
this.speed = speed;
}
public void move() {
}
public double getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public double getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public void speedUp() {
speed++;
}
public void speedDown() {
speed-=10;
}
public static void main(String args[]) {
Vehicle bike = new Vehicle(50, 100);
System.out.println("bike Speed is"+bike.getSpeed()+" size is"+bike.getSize());
bike.speedUp();
System.out.println("bike Speed is"+bike.getSpeed());
bike.speedDown();
System.out.println("bike Speed is"+bike.getSpeed());
}
}