课后练习题
//定义一个交通工具的父类Vehicle
public class Vehicle {
private String tool; //定义运输工具
private String way; //定义运输方式
private int amount; //定义运输人数
public void tranSport(String tool,String way,int amount){
this.tool = tool;
this.way = way;
this.amount = amount;
}
public void tranSport(){
System.out.println(tool+"可以在"+way+"载客"+amount+"人");
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------
//公共汽车子类
public class Bus extends Vehicle{
public Bus(){
super.tranSport("公共汽车","陆地",40);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------
//轮船子类
public class Steamship extends Vehicle{
public Steamship(){
super.tranSport("轮船","海上",200);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------
//飞机子类
public class Plane extends Vehicle{
public Plane(){
super.tranSport("飞机","天空",400);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------
//输出结果
public static void main(String[] args){
//利用对象的多态创建实例
Vehicle bus = new Bus();
bus.tranSport();
Vehicle steamship = new Steamship();
steamship.tranSport();
Vehicle plane = new Plane();
plane.tranSport();
}