weixin_52029326 2023-06-13 22:24 采纳率: 100%
浏览 109
已结题

编写一个Java应用程序,设计一个汽车类Vehicle

3.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。

  • 写回答

2条回答 默认 最新

  • Huazie 优质创作者: 编程框架技术领域 2023-06-13 23:11
    关注

    参考如下:

    /**
     * @author huazie
     * @version 2.0.0
     * @since 2.0.0
     */
    public class Test  {
    
        public static void main(String[] args) {
            Truck truck = new Truck(4, 1000, 5, 500);
            truck.display();
        }
    
    }
    
    class Vehicle {
        private int wheels; // 车轮个数
        private double weight; // 车重
    
        public Vehicle(int wheels, double weight) {
            this.wheels = wheels;
            this.weight = 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;
        }
    
        public void display() {
            System.out.println("Vehicle{" + "wheels=" + wheels + ", weight=" + weight + '}');
        }
    }
    
    class Car extends Vehicle {
        private int loader; // 载人数
    
        public Car(int wheels, double weight, int loader) {
            super(wheels, weight);
            this.loader = loader;
        }
    
        public int getLoader() {
            return loader;
        }
    
        public void setLoader(int loader) {
            this.loader = loader;
        }
    
        public void display() {
            super.display();
            System.out.println("Car{" + "loader=" + loader + '}');
        }
    
    }
    
    class Truck extends Car {
        private double payload; // 载重量
    
        public Truck(int wheels, double weight, int loader, double payload) {
            super(wheels, weight, loader);
            this.payload = payload;
        }
    
        public double getPayload() {
            return payload;
        }
    
        public void setPayload(double payload) {
            this.payload = payload;
        }
    
        public void display() {
            super.display();
            System.out.println("Truck{" + "payload=" + payload + '}');
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月22日
  • 已采纳回答 6月14日
  • 修改了问题 6月13日
  • 创建了问题 6月13日