qcode
  • 创作工场专业版,一问多答!更智能、更完整、更原创
  • 这是一个超强内容创作平台,它会写文章、做设计、绘图、敲代码,有问必答!
🔥点击使用-免登录深度版🔥

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

时间2023-06-20 18:30:45 提问来自 辽宁省铁岭市 的网友 分类创作 浏览266
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类作为子类,分别继承了Vehicle类的属性和方法,并且在子类中添加了自己的属性和方法。在Car类中添加了openable属性和getIn、getOut方法,在Truck类中添加了capacity属性和load、unload方法。

不满意?免费深度重写!

免责声明:
以上内容除特别注明外均来源于网友提问,创作工场回答,未经许可,严谨转载。

上一篇 怎么改变经常焦虑的心态 下一篇 发条橙 A Clockwork Orange影评