BZ易风

导航

 
 1 class HouseItem():
 2     """家具"""
 3     def __init__(self,name,area):
 4         self.name = name
 5         self.area = area
 6 
 7     def __str__(self):
 8         return ("%s的占地面积:%.2f" % (self.name, self.area))
 9 
10 class House():
11     """新房"""
12 
13     def __init__(self,house_type,area):
14         # 户型
15         self.house_type = house_type
16         # 总面积
17         self.area = area
18         # 剩余面积
19         self.free_area = self.area
20         # 家具名称列表
21         self.item_list = []
22 
23     def __str__(self):
24         return ("户型:%s\n总面积:%.2f[剩余面积:%.2f]\n家具:%s"
25                 % (self.house_type,self.area,self.free_area,self.item_list))
26 
27 
28     def add_item(self,item):
29         if item.area > self.free_area:
30             print("%s的面积太大了 家里放不下了" % item.name)
31             return
32         self.free_area -= item.area
33 
34         self.item_list.append(item.name)
35         # print("添加了:%s,占地面积:%.2f,剩余面积:%.2f" % (item.name,item.area,self.free_area))
36 
37 
38 bed = HouseItem("大床",40)
39 table = HouseItem("餐桌",25)
40 house = House("两室一厅",60)
41 print(bed)
42 house.add_item(bed)
43 house.add_item(table)
44 print(house)
大床的占地面积:40.00
餐桌的面积太大了 家里放不下了
户型:两室一厅
总面积:60.00[剩余面积:20.00]
家具:['大床']

  

posted on 2019-08-13 14:36  BZ易风  阅读(161)  评论(0编辑  收藏  举报