#29.編寫一個Java應用程序,設計一個汽車類Vehicle,包含的屬性有車輪個數
wheels和車重weight。小車類Car是Vehicle的子類,其中包含的屬性有載人數
loader。卡車類Truck是Car類的子類,其中包含的屬性有載重量payload。每個
類都有構造方法和輸出相關數據的方法。最后,寫一個測試類來測試這些類的功
能。
package hanqi; public class Vehicle { private int wheels; private int weight; public int getWheels() { return wheels; } public void setWheels(int wheels) { this.wheels = wheels; } public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } //構造 public Vehicle(int wheels,int weight) { this.weight=weight; this.wheels=wheels; } }
package hanqi; public class Car extends Vehicle{ private int loader; public int getLoader() { return loader; } public void setLoader(int loader) { this.loader = loader; } //構造 public Car(int wheels, int weight,int loader) { super(wheels,weight); this.loader=loader; } }
package hanqi; public class Truck extends Car{ private int payload; public int getPayload() { return payload; } public void setPayload(int payload) { this.payload = payload; } //構造 public Truck(int wheels, int weight, int loader, int payload) { super(wheels,weight,loader); this.payload=payload; } }
package hanqi; public class TestVehicle { public static void main(String[] args) { Vehicle a = new Vehicle(4,3); System.out.println("a有:"+a.getWheels()+"個輪子\t:"+a.getWeight()+"噸重"); Car b= new Car(4,3,4); System.out.println("b有:"+b.getWheels()+"個輪子\t:"+b.getWeight()+"噸重\t可以坐"+b.getLoader()+"個人"); Truck c= new Truck(6,10,5,10); System.out.println("c有:"+c.getWheels()+"個輪子\t:"+c.getWeight()+"噸重\t可以坐"+c.getLoader()+"個人\t載重"+c.getPayload()+"噸");
} }

