用java面向对象程序设计方法,编写代码,输出客车的基本信息(品牌、颜色、座位数)、货车的基本信息(品牌、颜色、载重量);
时间: 2023-07-04 20:26:37 浏览: 44
好的,以下是Java面向对象程序设计的代码:
```java
// 客车类
class Bus {
private String brand; // 品牌
private String color; // 颜色
private int seatNum; // 座位数
// 构造方法
public Bus(String brand, String color, int seatNum) {
this.brand = brand;
this.color = color;
this.seatNum = seatNum;
}
// 获取品牌
public String getBrand() {
return brand;
}
// 获取颜色
public String getColor() {
return color;
}
// 获取座位数
public int getSeatNum() {
return seatNum;
}
}
// 货车类
class Truck {
private String brand; // 品牌
private String color; // 颜色
private double load; // 载重量
// 构造方法
public Truck(String brand, String color, double load) {
this.brand = brand;
this.color = color;
this.load = load;
}
// 获取品牌
public String getBrand() {
return brand;
}
// 获取颜色
public String getColor() {
return color;
}
// 获取载重量
public double getLoad() {
return load;
}
}
// 测试类
public class Test {
public static void main(String[] args) {
Bus bus = new Bus("Yutong", "white", 50);
Truck truck = new Truck("Dongfeng", "red", 5.0);
// 输出客车的基本信息
System.out.println("客车信息:" + bus.getBrand() + ", " + bus.getColor() + ", " + bus.getSeatNum() + "座");
// 输出货车的基本信息
System.out.println("货车信息:" + truck.getBrand() + ", " + truck.getColor() + ", " + truck.getLoad() + "吨");
}
}
```
输出结果为:
```
客车信息:Yutong, white, 50座
货车信息:Dongfeng, red, 5.0吨
```
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.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)
![](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)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)