課后練習(xí)題
//定義一個(gè)交通工具的父類Vehicle
public class Vehicle {
? ? private String tool;? ? ?//定義運(yùn)輸工具
? ? private String way;? ? ?//定義運(yùn)輸方式
? ? private int amount;? ? ?//定義運(yùn)輸人數(shù)
? ? 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);
? ? }
}
-----------------------------------------------------------------------------------------------------------------------------------------------
//飛機(jī)子類
public class Plane extends Vehicle{
? ? public Plane(){
? ? ? ? super.tranSport("飛機(jī)","天空",400);
? ? }
}
-----------------------------------------------------------------------------------------------------------------------------------------------
//輸出結(jié)果
? ? public static void main(String[] args){
? ? ? ?//利用對(duì)象的多態(tài)創(chuàng)建實(shí)例
? ? ? ? Vehicle bus = new Bus();
? ? ? ? bus.tranSport();
? ? ? ? Vehicle steamship = new Steamship();
? ? ? ? steamship.tranSport();
? ? ? ? Vehicle plane = new Plane();
? ? ? ? plane.tranSport();
? ? }
2019-02-12
雖然你這么實(shí)現(xiàn)可以,但是實(shí)際并沒(méi)有用到多態(tài)
2019-02-03
不用這么麻煩直接創(chuàng)建一個(gè)父類然后寫一個(gè)方法再創(chuàng)建幾個(gè)子類然后分別重寫幾個(gè)方法最后創(chuàng)建一個(gè)測(cè)試類用父類來(lái)引用子類的方法就可以了
2019-01-27
Vehicle vehicle = new Bus();
vehicle.tranSport();
vehicle = new Steamship();
vehicle?.tranSport();
vehicle = new Plane();
vehicle?.tranSport();
2019-01-27
Vehicle vehicle = new Bus();
vehicle.tranSport();
vehicle = new Steamship();
steamship.tranSport();
vehicle = new Plane();
plane.tranSport();
用一個(gè)父類的引用就可以了,沒(méi)必要用三個(gè)