首页 > 代码库 > 作业-继承7

作业-继承7

编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数

wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数

loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个

类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功

能。

package T092206;public class Vehicle {    private int wheels;    private double 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;    }    Vehicle(){            }    Vehicle(int wheels,double weight){        this.wheels=wheels;        this.weight=weight;    }    public void getInfo(){        System.out.println("车轮的个数是:"+wheels+" 车重:"+weight);    }}package T092206;public class Car extends Vehicle {    private int loader;    public int getLoader() {        return loader;    }    public void setLoader(int loader) {        this.loader = loader;    }    Car(){            }    Car(int wheels,double weight,int loader){        super(wheels,weight);        this.loader=loader;    }    public void loader(){        if(loader<=6){            System.out.println("这辆车能载"+loader+"人");        }        else{            System.out.println("超员了");        }    }}package T092206;public class Truck extends Car {    Truck(){        super();    }    private double payload;    public double getPayload() {        return payload;    }    public void setPayload(double payload) {        this.payload = payload;    }    public void payload(){        if(payload<=1000){            System.out.println("这辆车的载重是"+payload);        }        else{            System.out.println("超重了");        }    }}package T092206;public class Test {    public static void main(String[] args){        Car c=new Car(4,1000,4);        c.getInfo();        c.loader();        Truck t =new Truck();        t.setLoader(3);        t.setPayload(2000);        t.setWeight(3000);        t.setWheels(6);        t.getInfo();        t.loader();        t.payload();        }}

技术分享

作业-继承7