请编写程序实现汽车类vehicle一vehicle类包含私有成员变量车牌号numberString类型和品牌brand(String类型)二声明一个无参的构造方法初始化所有成员变量为“”;三声明一个有参的构造方法初始化车牌号为12345品牌为大众四定义两个成员变量的get和set方法五定义to string()方法按照“车牌号∶品牌”的格式输出二定义测试类创建Vehicle类的对象并输出车辆的信息
时间: 2023-04-03 15:01:39 浏览: 125
以下是CSDN开发的C知道的回答:
请看下面的代码实现:
```
public class Vehicle {
private String number; // 车牌号
private String brand; // 品牌
public Vehicle() {
this.number = "";
this.brand = "";
}
public Vehicle(String number, String brand) {
this.number = number;
this.brand = brand;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
@Override
public String toString() {
return "车牌号:" + number + ",品牌:" + brand;
}
}
public class TestVehicle {
public static void main(String[] args) {
Vehicle vehicle = new Vehicle("12345", "大众");
System.out.println(vehicle.toString());
}
}
```
输出结果为:
```
车牌号:12345,品牌:大众
```
相关推荐
![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)
![](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)