建立一个基类Building,用来存储一座楼房的层数、房间数以及它的总平方英尺数。建立派生类Housing,继承Building,并存储下面的内容:卧室和浴室的数量,另外,建立派生类Office,继承Building,并存储灭火器和电话的数目。然后,编制应用程序,建立住宅楼对象和办公楼对象,并输出它们的有关数据。
时间: 2024-03-11 20:51:49 浏览: 25
好的,根据您的要求,我会为您编写一个基类Building和两个派生类Housing和Office的代码。以下是示例代码:
```python
class Building:
def __init__(self, floors, rooms, sqft):
self.floors = floors
self.rooms = rooms
self.sqft = sqft
class Housing(Building):
def __init__(self, floors, rooms, sqft, bedrooms, bathrooms):
super().__init__(floors, rooms, sqft)
self.bedrooms = bedrooms
self.bathrooms = bathrooms
class Office(Building):
def __init__(self, floors, rooms, sqft, extinguishers, phones):
super().__init__(floors, rooms, sqft)
self.extinguishers = extinguishers
self.phones = phones
# 创建住宅楼对象
house = Housing(5, 20, 10000, 10, 8)
print(f"Housing: {house.floors} floors, {house.rooms} rooms, {house.sqft} sqft, {house.bedrooms} bedrooms, {house.bathrooms} bathrooms")
# 创建办公楼对象
office = Office(10, 50, 20000, 20, 50)
print(f"Office: {office.floors} floors, {office.rooms} rooms, {office.sqft} sqft, {office.extinguishers} extinguishers, {office.phones} phones")
```
运行以上代码,输出结果如下:
```
Housing: 5 floors, 20 rooms, 10000 sqft, 10 bedrooms, 8 bathrooms
Office: 10 floors, 50 rooms, 20000 sqft, 20 extinguishers, 50 phones
```
以上是根据您的要求编写的代码,如果您有其他问题或需求,可以随时告诉我。
阅读全文