練習題這么做可以吧???反正是運行出來了
父類Vehicle代碼:
package com.imooc;
public class Vehicle {
?? ?public String way;
?? ?public int num;
?? ?public String name;
?? ?public static void getWay(){
?? ??? ?System.out.println("各交通工具的運輸路徑分別是:");
?? ?}
?? ?public static void getNum(){
?? ??? ?System.out.println("各交通工具的承載人數分別是:");
?? ?}
}
子類car代碼:
package com.imooc;
public class Car extends Vehicle {
?? ?public int num=20;
}
測試類Initailv代碼:
package com.imooc;
public class Initailv {
?? ?public static void main(String[] args) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?Vehicle car=new Vehicle();
?? ??? ?car.name="汽車";
?? ??? ?car.way="陸地";
?? ??? ?car.num=20;
?? ??? ?Vehicle ship=new Vehicle();
?? ??? ?ship.name="輪船";
?? ??? ?ship.way="海洋";
?? ??? ?ship.num=200;
?? ??? ?Vehicle plane=new Vehicle();
?? ??? ?plane.name="飛機";
?? ??? ?plane.way="空中";
?? ??? ?plane.num=500;
?? ??? ?Vehicle.getWay();
?? ??? ?System.out.println(car.name+":"+car.way);
?? ??? ?System.out.println(ship.name+":"+ship.way);
?? ??? ?System.out.println(plane.name+":"+plane.way);
?? ??? ?Vehicle.getNum();
?? ??? ?System.out.println(car.name+":"+car.num);
?? ??? ?System.out.println(ship.name+":"+ship.num);
?? ??? ?System.out.println(plane.name+":"+plane.num);
?? ?}
}
2016-01-14
可以了,但是不夠簡潔,后面學習到封裝,繼承和多態會更加簡潔的,代碼的維護度更強
2016-04-11
沒有用到封裝
2016-01-18
倒數第六行和倒數第十行,調用方法要用對象。你用的是類。
2016-01-14
寫的太復雜了,但是是正確的