酷代码 AI
菜单
全部AI  /  热门  /  AI问答
服务商

题目一:继承(汽车类继承) **题目描述**: 创建一个`Vehicle`(车辆)基类,包含`brand`(品牌)和`price`(价格)属性,以及`displayInfo()`方法用于显示车辆信息。然后创建`Car`(汽车)子类继承`Vehicle`,添加`numDoors`(车门数量)属性,并重写`displayInfo()`方法以显示所有信息。 **要求**: 1. 使用适当的访问修饰符 2. 在子类中1)使用`super`调用父类构造方法 3. 实现方法重写 **输出内容**: Brand: Toyota Price: $25000.0 Number of doors: 4

```java class Vehicle { private String brand; private double price; public Vehicle(String brand, double price) { this.brand = brand; this.price = price; } public void displayInfo() { System.out.println("Brand: " + brand); System.out.println("Price: $" + price); } } class Car extends Vehicle { private int numDoors; public Car(String brand, double price, int numDoors) { super(brand, price); this.numDoors = numDoors; } @Override public void displayInfo() { super.displayInfo(); System.out.println("Number of doors: " + numDoors); } public static void main(String[] args) { Car car = new Car("Toyota", 25000.0, 4); car.displayInfo(); } } ``` [2025-04-15 14:38:26 | AI问答 | 220点数解答]

相关提问