聲明一個抽像交通工具類及其子類陸地交通工具
聲明一個抽像交通工具類及其子類陸地交通工具,再定義陸地交通工具的兩個子類汽車類與自行車類。
public class AutoMobile extends LandVehicle
{
//double speed;
public void state()
{
System.out.println("Now the speed of the autoMobile is "+speed);
}
public static void main(String[] args)
{
AutoMobile am=new AutoMobile();
am.start();
am.speedUp(15.0);
am.state();
am.speedDown(10.4);
am.state();
am.stop();
}
}
public class Bike extends LandVehicle
{
//double speed;
public void state()
{
System.out.println("Now the speed of the Bike is "+speed);
}
public static void main(String[] args)
{
AutoMobile am=new AutoMobile();
am.start();
am.speedUp(30.4);
am.state();
am.speedDown(10.4);
am.state();
am.stop();
}
}
abstract class Vehicle
{
//public float speed;
abstract void start();
abstract void stop();
abstract double speedUp(double increaseSpeed);
abstract void state();
abstract double speedDown(double decreaseSpeed);
}
public class LandVehicle extends Vehicle
{
double speed;
public void start()
{
System.out.println("Now Start!");
}
public void state()
{
System.out.println("Now the speed of the vehicle is "+speed);
}
public void stop()
{
System.out.println("Now Stop!");
}
public double speedUp(double increaseSpeed)
{
speed=speed+increaseSpeed;
return speed;
}
public double speedDown(double decreaseSpeed)
{
if(speed>=decreaseSpeed)
speed=speed-decreaseSpeed;
return speed;
}
/*public static void main(String[] args)
{
LandVehicle lv=new LandVehicle();
lv.start();
lv.speedUp(30.0);
lv.state();
lv.speedDown(15.9);
lv.stop();
lv.state();
}*/
}
|