定义一个汽车Car类,要求如下: (1)有三个属性,并且属性为私有的。 (2)至少有一个有参的构造方法。 (3)为私有属性提供访问器方法。 定义测试类,显示汽车的基本信息。
时间: 2023-07-16 17:14:27 浏览: 48
下面是Car类的实现:
```java
public class Car {
private String brand;
private String model;
private int year;
public Car(String brand, String model, int year) {
this.brand = brand;
this.model = model;
this.year = year;
}
public String getBrand() {
return brand;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
}
```
下面是测试类的实现:
```java
public class CarTest {
public static void main(String[] args) {
Car car = new Car("Toyota", "Camry", 2021);
System.out.println("Brand: " + car.getBrand());
System.out.println("Model: " + car.getModel());
System.out.println("Year: " + car.getYear());
}
}
```
输出结果为:
```
Brand: Toyota
Model: Camry
Year: 2021
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)