Ø 属性:速度(speed),体积(size)等
Ø 方法:移动(move()),设置速度(setSpeed(intspeed)),设置体积(setSize(int size))加速speedUp(),减速speedDown()等
在测试类Vehicle中的main()中实例化一个交通工具对象,通过方法给它初始化speed,size的值,并打印出来。另外,调用加速,减速的方法对速度进行改变。
public class Vehicle {
int speed;
int size;
void speedUp()
{
speed=speed+1;
}
void speedDown()
{
speed=speed-1;
}
void SetSpeed(int speed)
{
this.speed=speed;
}
void SetSize(int size)
{
this.size=size;
}
void move()
{
System.out.println("汽车正在行驶!!!");
}
}
public class TestVehicle {
public static void main(String[] args) {
// TODO Auto-generated method stub
Vehicle vehicle=new Vehicle();
vehicle.speed=5;
vehicle.size=7;
vehicle.move();
System.out.println("当前的速度是"+vehicle.speed);
vehicle.speedUp();
System.out.println("当前的速度是"+vehicle.speed);
vehicle.speedDown();
System.out.println("当前的速度是"+vehicle.speed);
}
}