用java:创建一个Vehicle类并将它声明为抽象类。在Vehicle类中声明两个抽象方法run和speed。创建两个类Car 和 Motorbike 从Vehicle类继承,并在这两个类中实现抽象类方法。重写的方法自己定义方法的操作。设计主类,在该类中创建Car和 Motorbike的实例,进行上述重写方法的测试。
时间: 2024-02-06 21:13:07 浏览: 38
好的,以下是您要求的Java代码:
```java
abstract class Vehicle {
abstract void run();
abstract void speed();
}
class Car extends Vehicle {
@Override
void run() {
System.out.println("Car is running.");
}
@Override
void speed() {
System.out.println("Car speed is 60 km/h.");
}
}
class Motorbike extends Vehicle {
@Override
void run() {
System.out.println("Motorbike is running.");
}
@Override
void speed() {
System.out.println("Motorbike speed is 80 km/h.");
}
}
public class Main {
public static void main(String[] args) {
Vehicle car = new Car();
Vehicle motorbike = new Motorbike();
car.run();
car.speed();
motorbike.run();
motorbike.speed();
}
}
```
首先,我们定义了一个抽象类`Vehicle`,其中包含两个抽象方法`run`和`speed`。然后,我们创建了两个子类`Car`和`Motorbike`,它们继承了`Vehicle`类,并实现了抽象方法。最后,我们在主类`Main`中创建了`Car`和`Motorbike`的实例,并测试了它们的重写方法。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)