用java编写一个Car类,具有final类型的属性品牌,具有功能drive;定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速;定义自己名字+学号的主类,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特性。
时间: 2024-03-11 14:46:42 浏览: 41
好的,以下是Java代码实现:
```java
public class Car {
private final String brand;
public Car(String brand) {
this.brand = brand;
}
public String getBrand() {
return brand;
}
public void drive() {
System.out.println("我正在开车!");
}
}
class Aodi extends Car {
private double price;
private String model;
public Aodi(String brand, double price, String model) {
super(brand);
this.price = price;
this.model = model;
}
public double getPrice() {
return price;
}
public String getModel() {
return model;
}
public void changeGear() {
System.out.println("我正在换挡!");
}
}
class Benchi extends Car {
private double price;
private String model;
public Benchi(String brand, double price, String model) {
super(brand);
this.price = price;
this.model = model;
}
public double getPrice() {
return price;
}
public String getModel() {
return model;
}
public void changeGear() {
System.out.println("我正在换挡!");
}
}
public class Main {
public static void main(String[] args) {
Aodi aodi = new Aodi("奥迪", 500000, "A6");
Benchi benchi = new Benchi("奔驰", 600000, "S500");
System.out.println(aodi.getBrand() + " " + aodi.getModel() + " 价格:" + aodi.getPrice());
System.out.println(benchi.getBrand() + " " + benchi.getModel() + " 价格:" + benchi.getPrice());
aodi.changeGear();
benchi.changeGear();
aodi.drive();
benchi.drive();
}
}
```
运行结果如下:
```
奥迪 A6 价格:500000.0
奔驰 S500 价格:600000.0
我正在换挡!
我正在换挡!
我正在开车!
我正在开车!
```
以上就是根据您的要求编写的Java程序,实现了Car类及其子类Aodi和Benchi,分别具有属性和功能,同时在主类中创建对象并测试特性。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)