欢迎访问大师范文AI写作助手,如没有想要的内容请与本站万能的AI助手联系。

java Vehicle 类作为父类,属性有 string engine 和 int wheelNum,使用的方法有 void run()和 void stop()。Car类为子类,属性有 boole

分享 时间: 加入收藏 我要投稿 点赞
oad(num:int):void。

Vehicle类的代码如下:

```java
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类的代码如下:

```java
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类的代码如下:

```java
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类的基本代码,可以根据需要进行扩展和修改。

精选图文

221381
领取福利

微信扫码领取福利

java Vehicle 类作为父类,属性有 string engine 和 int wheelNum,使用的方法有 void run()和 void stop()。Car类为子类,属性有 boole

微信扫码分享