【问题描述】设计一个建筑物类 Building,由它派生出教学楼类 Teach-Building 和宿舍楼类 Dorm-Building,前者包括教学楼编号、层数、教室数、总面积等基本信息,后者包括宿舍楼编号、层数、宿舍数、总面积和容纳学生总人数等基本信息。根据教学楼和宿舍楼属性设计出 Building 类,在类中完成输出每种建筑物的属性。编写无参数的构造函数初始化两种楼信息。 【输入形式】首先输入教学楼信息,再输入宿舍楼信息,信息之间用单个空格分隔 【输出形式】先输出宿舍楼信息,再输出教学楼信息 【样例输入】 mainteachbuild 56 6 66 18000 北苑男生宿舍 ",41,5,75,3750,300 southmaledormitory 41 5 75 3750 300 【样例输出】 southmaledormitory 41 5 75 3750 300 mainteachbuild 56 6 66 18000
时间: 2023-11-28 19:47:47 浏览: 43
```python
class Building:
def __init__(self, building_id, floors, area):
self.building_id = building_id
self.floors = floors
self.area = area
def display_info(self):
print(self.building_id, self.floors, self.area)
class TeachBuilding(Building):
def __init__(self, building_id, floors, area, classrooms):
super().__init__(building_id, floors, area)
self.classrooms = classrooms
def display_info(self):
super().display_info()
print(self.classrooms)
class DormBuilding(Building):
def __init__(self, building_id, floors, area, dorms, capacity):
super().__init__(building_id, floors, area)
self.dorms = dorms
self.capacity = capacity
def display_info(self):
super().display_info()
print(self.dorms, self.capacity)
dorm_info = input().split()
dorm_building = DormBuilding(dorm_info[0], int(dorm_info[1]), int(dorm_info[2]), int(dorm_info[3]), int(dorm_info[4]))
dorm_building.display_info()
teach_info = input().split()
teach_building = TeachBuilding(teach_info[0], int(teach_info[1]), int(teach_info[2]), int(teach_info[3]))
teach_building.display_info()
```
解析:
1. 首先定义了一个 `Building` 类,包含 `building_id`、`floors` 和 `area` 三个属性,分别表示建筑物编号、层数和总面积。同时定义了一个 `display_info` 方法,用于输出建筑物的属性。
2. 在 `TeachBuilding` 类中,继承了 `Building` 类,新增了 `classrooms` 属性,表示教室数量。重写了 `display_info` 方法,先调用父类的 `display_info` 方法输出基本属性,再输出教室数量。
3. 在 `DormBuilding` 类中同样继承了 `Building` 类,新增了 `dorms` 和 `capacity` 两个属性,分别表示宿舍数量和容纳学生总人数。同样重写了 `display_info` 方法,先调用父类的 `display_info` 方法输出基本属性,再输出宿舍数量和容纳学生总人数。
4. 在主程序中,首先读入宿舍楼信息,创建 `DormBuilding` 对象并调用 `display_info` 方法输出宿舍楼信息。然后读入教学楼信息,创建 `TeachBuilding` 对象并调用 `display_info` 方法输出教学楼信息。
相关推荐
![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)
![](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)