编写一个Car类,代表小汽车。要求如下:成员属性:商标brand、颜色color和座位seats (访问权限均为private)。构造方法:有一个无参构造方法,初始化一辆5座的白色大众汽车;有一个带3个参数的构造方法,所有的属性
时间: 2023-06-25 14:08:04 浏览: 70
都由参数传入。成员方法:getBrand()、getColor()、getSeats()分别用于获取品牌、颜色和座位数。setBrand()、setColor()、setSeats()分别用于设置品牌、颜色和座位数。具体实现如下:
```java
public class Car {
private String brand;
private String color;
private int seats;
public Car() {
this.brand = "大众";
this.color = "白色";
this.seats = 5;
}
public Car(String brand, String color, int seats) {
this.brand = brand;
this.color = color;
this.seats = seats;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getSeats() {
return seats;
}
public void setSeats(int seats) {
this.seats = seats;
}
}
```
这样就完成了一个简单的Car类。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)