Java基础-继承-编写⼀个Java应⽤程序,设计⼀个汽车类
Vehicle,包含的属性有车。。。
#29.编写⼀个Java应⽤程序,设计⼀个汽车类Vehicle,包含的属性有车轮个数
wheels和车重weight。⼩车类Car是Vehicle的⼦类,其中包含的属性有载⼈数
weight的所有形式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()+"吨"); }
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。