JAVA|设计一个汽车类Vehicle,小车类Car是Vehicle的子类,卡车类Truck是Car类的子类

设计一个汽车类Vehicle,小车类Car是Vehicle的子类,卡车类Truck是Car类的子类

编写一个Java应用程序

设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。

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

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

每个类都有构造方法和输出相关数据的方法。

最后,写一个测试类来测试这些类的功能。

Vehicle.java👇

package work.tenth;

public class Vehicle {
    protected int wheels;
    protected double weight;

    public Vehicle(int wheels, double weight) {
        this.wheels = wheels;
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "Vehicle[wheels=" + wheels + ", weight=" + weight + "]";
    }
}

Vehicle.java👇

package work.tenth;

public class Car extends Vehicle {
    protected int loader;

    public Car(int wheels, double weight, int loader) {
        super(wheels, weight);
        this.loader = loader;
    }

    @Override
    public String toString() {
        return "Car[loader=" + loader + ", wheels=" + wheels + ", weight=" + weight +"]";
    }
}

Truck.java👇

package work.tenth;

public class Truck extends Car {
    private double payload;

    public Truck(int wheels, double weight, int loader, double payload) {
        super(wheels, weight, loader);
        this.payload = payload;
    }

    @Override
    public String toString() {
        return "Truck[" +
                "payload=" + payload +
                ", loader=" + loader +
                ", wheels=" + wheels +
                ", weight=" + weight +
                "]";
    }
}

Test.java👇

package work.tenth;

public class Test {
    public static void main(String[] args) {
        Truck truck = new Truck(8,500,4,400);
        System.out.println(truck);
        Car car = new Car(4,200,3);
        System.out.println(car);
        Vehicle vehicle = new Vehicle(2,300);
        System.out.println(vehicle);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值