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

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

参考如下:

/**
 * @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 + '}');
    }

}
不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 以下回答来自chatgpt:

    我需要了解关于Java应用程序的汽车类的设计。我需要了解汽车类的具体属性和方法,以便在代码中使用该类。可以考虑以下属性:座位数、颜色、型号、制造商、速度和当前状态等。对于方法,可以考虑启动、停止、加速、刹车、获取当前速度等。请以面向对象的思想设计这个汽车类,并提供该类的实现代码。以下是参考实现:

    public class Car {
        private int seats;
        private String color;
        private String model;
        private String manufacturer;
        private int speed;
        private boolean isRunning;
    
        public Car(int numSeats, String carColor, String carModel, String carManufacturer) {
            seats = numSeats;
            color = carColor;
            model = carModel;
            manufacturer = carManufacturer;
            speed = 0;
            isRunning = false;
        }
    
        public void start() {
            if(!isRunning) {
                isRunning = true;
                System.out.println("The car has started.");
            } else {
                System.out.println("The car is already running.");
            }
        }
    
        public void stop() {
            if(isRunning) {
                isRunning = false;
                speed = 0;
                System.out.println("The car has stopped.");
            } else {
                System.out.println("The car is already stopped.");
            }
        }
    
        public void accelerate(int increment) {
            if(isRunning) {
                speed += increment;
                System.out.println("The car's speed is now " + speed + " mph.");
            } else {
                System.out.println("The car needs to be started first.");
            }
        }
    
        public void brake(int decrement) {
            if(isRunning) {
                if(speed - decrement >= 0) {
                    speed -= decrement;
                } else {
                    speed = 0;
                }
                System.out.println("The car's speed is now " + speed + " mph.");
            } else {
                System.out.println("The car needs to be started first.");
            }
        }
    
        public int getSpeed() {
            return speed;
        }
    }
    

    以上就是一个简单的汽车类的实现。可以根据需求添加更多属性和方法,例如获取当前状态等。在使用该类时,可以创建一个Car对象并调用对象的方法来操作汽车。例如:

    Car myCar = new Car(4, "red", "sedan", "Honda");
    myCar.start();
    myCar.accelerate(30);
    myCar.brake(10);
    myCar.stop();
    

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^