4.1后面的老师讲的练习题,求程序代码??
package Vehicle; public class Vehicle { public void transport(){ System.out.println("交通工具的运输方式"); } public static void main(String args[]){ Vehicle v1 = new Car(); Vehicle v2 = new Bus(); Vehicle v3 = new Launch(); Vehicle v4 = new Ship(); Vehicle v5 = new Airplane(); v1.transport(); v2.transport(); v3.transport(); v4.transport(); v5.transport(); } }
package Vehicle; public class Car extends Vehicle { public void transport(){ System.out.println("小汽车可以在陆上运输5个人"); } }
package Vehicle; public class Bus extends Vehicle { public void transport(){ System.out.println("公共汽车可以在陆上运输40个人"); } }
package Vehicle; public class Launch extends Vehicle { public void transport(){ System.out.println("汽艇可以在海上运输10个人"); } }
package Vehicle; public class Ship extends Vehicle { public void transport(){ System.out.println("轮船可以在海上运输200个人"); } }
package Vehicle; public class Airplane extends Vehicle { public void transport(){ System.out.println("飞机可以在天上运输100个人"); } }
简单明了,紧抓这节 多态要点。666