首页 > 代码库 > 车的轮胎数 重量

车的轮胎数 重量

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

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

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

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

能。

package zuoye;public class Vehicle {    private int wheels;    private double weight;    Vehicle()    {    weight=1;wheels=4;    }    Vehicle(int geshu, double chezhong)    {    wheels=geshu;    weight=chezhong;    }    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 shuchu(){    System.out.println("轮子"+wheels+"个,"+"车重"+weight);    }}
{    private int loader;    Car1()    {    loader=4;    }    Car1(int zaike)    {    loader=zaike;    }    Car1(int geshu, double chezhong,int zaike)    {    super(geshu,chezhong);    loader=zaike;    }    public void showData(){    System.out.println("轮子"+getWheels()+"个"+"自重"+getWeight());    System.out.println("载客人数:"+loader+"人");    }    public int getLoader() {    return loader;    }    public void setLoader(int loader) {    this.loader = loader;    }}
package zuoye;public class Truck extends Car1{    private double payload;    Truck()    {    payload=500;    }    Truck(double zaizhong)    {    payload=zaizhong;    }    Truck(int zaike,double zaizhong)    {    super(zaike);    payload=zaizhong;    }    Truck(int lun, double chezhong,int zaike,double zaizhong)    {    super(lun,chezhong,zaike);    payload=zaizhong;    }    public void showData(){    System.out.println("轮子"+getWheels()+"个"+"自重"+getWeight());    System.out.println("载客人数:"+getLoader()+"人");    System.out.println("载重:"+payload);    }}
package zuoye;public class ceshi2 {    public static void main(String[] args)     {        Truck t=new Truck();        t.showData();        t=new Truck(500);        t.showData();        t=new Truck(20,500);        t.showData();        t=new Truck(20,21,22,500);        t.showData();    }}

技术分享

 

车的轮胎数 重量