Java基础-继承-编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个 类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功 能。
#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()+"吨");
}
}