用java创建一个名为Vehicle的抽象类,具有一个名为start()的抽象方法和一个名为stop()的具体方法。创建两个子类Car和Motorcycle,并实现它们的start()方法。在Main类中创建一个Vehicle数组并用各种不同类型的Vehicle对象填充它,然后循环遍历该数组并分别调用每个Vehicle对象的start()和stop()方法。
时间: 2023-12-14 20:36:00 浏览: 181
好的,以下是Java代码实现:
```java
public abstract class Vehicle {
public abstract void start();
public void stop() {
System.out.println("Vehicle stopped.");
}
}
public class Car extends Vehicle {
@Override
public void start() {
System.out.println("Car started.");
}
}
public class Motorcycle extends Vehicle {
@Override
public void start() {
System.out.println("Motorcycle started.");
}
}
public class Main {
public static void main(String[] args) {
Vehicle[] vehicles = new Vehicle[3];
vehicles[0] = new Car();
vehicles[1] = new Motorcycle();
vehicles[2] = new Car();
for(Vehicle vehicle: vehicles) {
vehicle.start();
vehicle.stop();
}
}
}
```
运行结果如下:
```
Car started.
Vehicle stopped.
Motorcycle started.
Vehicle stopped.
Car started.
Vehicle stopped.
```
阅读全文