用Java(1)定义车类( Car) 成员变量:品牌( brand )、价格(price)、颜色(colour) 构造方法:无参、带参 成员方法:get/set方法、show( ) 抽象方法 function(); (2)定义跑车类(SportsCar) 构造方法:无参、带参 成员方法:重写function(){…},并输出一条语句“超级跑车” (3)货车类(van) 成员变量:车轮数(wheels) 构造方法:无参、带参 成员方法:get/set方法,重写function(){…},并输出一条语句“大型货车” (4)定义测试类(CarDemo),使用无参创建对象和有参创建对象写代码测试
时间: 2024-03-05 18:49:33 浏览: 36
好的,以下是Java代码实现:
1. Car类
```java
public class Car {
private String brand;
private double price;
private String colour;
public Car() {
}
public Car(String brand, double price, String colour) {
this.brand = brand;
this.price = price;
this.colour = colour;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getColour() {
return colour;
}
public void setColour(String colour) {
this.colour = colour;
}
public void show() {
System.out.println("Brand: " + brand + ", Price: " + price + ", Colour: " + colour);
}
public abstract void function();
}
```
2. SportsCar类
```java
public class SportsCar extends Car {
public SportsCar() {
}
public SportsCar(String brand, double price, String colour) {
super(brand, price, colour);
}
@Override
public void function() {
System.out.println("Super Sports Car");
}
}
```
3. Van类
```java
public class Van extends Car {
private int wheels;
public Van() {
}
public Van(String brand, double price, String colour, int wheels) {
super(brand, price, colour);
this.wheels = wheels;
}
public int getWheels() {
return wheels;
}
public void setWheels(int wheels) {
this.wheels = wheels;
}
@Override
public void function() {
System.out.println("Large Van");
}
}
```
4. CarDemo类
```java
public class CarDemo {
public static void main(String[] args) {
Car car1 = new SportsCar();
car1.setBrand("Ferrari");
car1.setPrice(1000000);
car1.setColour("Red");
car1.show();
car1.function();
Car car2 = new Van("Volvo", 80000, "White", 6);
car2.show();
car2.function();
}
}
```
输出结果如下:
```
Brand: Ferrari, Price: 1000000.0, Colour: Red
Super Sports Car
Brand: Volvo, Price: 80000.0, Colour: White
Large Van
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)