请使用java创建几个具有抽象方法enginePower实现的汽车品牌类,例如BMW、Audi、Honda等。 在名为Cars类中声明抽象方法。为每个汽车品牌创建对象,并打印出这些汽车的发动机功率。
时间: 2023-06-27 22:01:46 浏览: 87
设计一个Car类
以下是示例代码:
```
abstract class Cars {
public abstract int enginePower();
}
class BMW extends Cars {
public int enginePower() {
return 300;
}
}
class Audi extends Cars {
public int enginePower() {
return 280;
}
}
class Honda extends Cars {
public int enginePower() {
return 200;
}
}
public class Main {
public static void main(String[] args) {
Cars bmw = new BMW();
Cars audi = new Audi();
Cars honda = new Honda();
System.out.println("BMW engine power: " + bmw.enginePower());
System.out.println("Audi engine power: " + audi.enginePower());
System.out.println("Honda engine power: " + honda.enginePower());
}
}
```
输出结果:
```
BMW engine power: 300
Audi engine power: 280
Honda engine power: 200
```
阅读全文