定义一个名为Vehicle的接口,其中包括两个方法:startl()和stop()。另外设计两个类, 分别为Bike和Bus,然后在这两个类中实现Vehicle接口。设计一个名为interfaceDemo的主类, 在它的main()方法中创建Bike和Bus的对象,并访问start()和stop()方法
时间: 2024-03-21 08:38:52 浏览: 23
好的,我理解了你的问题。下面是Java语言的代码实现:
```
// 定义Vehicle接口
interface Vehicle {
void start();
void stop();
}
// 实现Bike类,实现Vehicle接口
class Bike implements Vehicle {
public void start() {
System.out.println("Bike started!");
}
public void stop() {
System.out.println("Bike stopped!");
}
}
// 实现Bus类,实现Vehicle接口
class Bus implements Vehicle {
public void start() {
System.out.println("Bus started!");
}
public void stop() {
System.out.println("Bus stopped!");
}
}
// 主类interfaceDemo
public class interfaceDemo {
public static void main(String[] args) {
// 创建Bike对象并访问start()和stop()方法
Vehicle bike = new Bike();
bike.start();
bike.stop();
// 创建Bus对象并访问start()和stop()方法
Vehicle bus = new Bus();
bus.start();
bus.stop();
}
}
```
运行上述代码,输出结果为:
```
Bike started!
Bike stopped!
Bus started!
Bus stopped!
```
希望这个例子可以帮助你更好地理解Java中接口的概念和用法。