aiaiask.com©

开花即结果 提问即答案

Java中Vehicle类作父类含引擎与轮数属性及运行停止方法,Car和Truck子类各有特有属性及方法
原始问题:

Java中Vehicle类作父类含引擎与轮数属性及运行停止方法,Car和Truck子类各有特有属性及方法

oad(num:int):void。

Vehicle类的代码如下:

public class Vehicle {
    private String engine;
    private int wheelNum;

    public Vehicle(String engine, int wheelNum) {
        this.engine = engine;
        this.wheelNum = wheelNum;
    }

    public void run() {
        System.out.println("Vehicle is running.");
    }

    public void stop() {
        System.out.println("Vehicle has stopped.");
    }
}

Car类的代码如下:

public class Car extends Vehicle {
    private boolean openable;

    public Car(String engine, int wheelNum, boolean openable) {
        super(engine, wheelNum);
        this.openable = openable;
    }

    public void getIn(int num) {
        System.out.println(num + " people get in the car.");
    }

    public void getOut(int num) {
        System.out.println(num + " people get out of the car.");
    }
}

Truck类的代码如下:

public class Truck extends Vehicle {
    private int capacity;

    public Truck(String engine, int wheelNum, int capacity) {
        super(engine, wheelNum);
        this.capacity = capacity;
    }

    public void load(int num) {
        System.out.println("Load " + num + " goods.");
    }

    public void unload(int num) {
        System.out.println("Unload " + num + " goods.");
    }
}

以上是基于题目提供的要求编写的代码,其中Vehicle类作为父类,Car和Truck类作为子类,分别继承了Vehicle类的属性和方法,并且在子类中添加了自己的属性和方法。在Car类中添加了openable属性和getIn、getOut方法,在Truck类中添加了capacity属性和load、unload方法。

Java中Vehicle类作父类含引擎与轮数属性及运行停止方法,Car和Truck子类各有特有属性及方法
https://www.aiaiask.com/ask/38454.html
AI / 模型iFLYTEK
时间 / DATEJune 20, 2023, 6:30 PM
语言zh-CN
IP / 区域辽宁 铁岭