練習(xí)題這么做可以吧???反正是運(yùn)行出來(lái)了
父類Vehicle代碼:
package com.imooc;
public class Vehicle {
?? ?public String way;
?? ?public int num;
?? ?public String name;
?? ?public static void getWay(){
?? ??? ?System.out.println("各交通工具的運(yùn)輸路徑分別是:");
?? ?}
?? ?public static void getNum(){
?? ??? ?System.out.println("各交通工具的承載人數(shù)分別是:");
?? ?}
}
子類car代碼:
package com.imooc;
public class Car extends Vehicle {
?? ?public int num=20;
}
測(cè)試類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="飛機(jī)";
?? ??? ?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
可以了,但是不夠簡(jiǎn)潔,后面學(xué)習(xí)到封裝,繼承和多態(tài)會(huì)更加簡(jiǎn)潔的,代碼的維護(hù)度更強(qiáng)
2016-04-11
沒(méi)有用到封裝
2016-01-18
倒數(shù)第六行和倒數(shù)第十行,調(diào)用方法要用對(duì)象。你用的是類。
2016-01-14
寫(xiě)的太復(fù)雜了,但是是正確的