3.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。
2条回答 默认 最新
关注参考如下:
/** * @author huazie * @version 2.0.0 * @since 2.0.0 */ public class Test { public static void main(String[] args) { Truck truck = new Truck(4, 1000, 5, 500); truck.display(); } } class Vehicle { private int wheels; // 车轮个数 private double weight; // 车重 public Vehicle(int wheels, double weight) { this.wheels = wheels; this.weight = weight; } public int getWheels() { return wheels; } public void setWheels(int wheels) { this.wheels = wheels; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public void display() { System.out.println("Vehicle{" + "wheels=" + wheels + ", weight=" + weight + '}'); } } class Car extends Vehicle { private int loader; // 载人数 public Car(int wheels, double weight, int loader) { super(wheels, weight); this.loader = loader; } public int getLoader() { return loader; } public void setLoader(int loader) { this.loader = loader; } public void display() { super.display(); System.out.println("Car{" + "loader=" + loader + '}'); } } class Truck extends Car { private double payload; // 载重量 public Truck(int wheels, double weight, int loader, double payload) { super(wheels, weight, loader); this.payload = payload; } public double getPayload() { return payload; } public void setPayload(double payload) { this.payload = payload; } public void display() { super.display(); System.out.println("Truck{" + "payload=" + payload + '}'); } }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报