标签:self div 子类 面向对象 name 面向 code __init__ use
#coding = UTF-8 class House: def __init__(self,house_type ,area ): self.house_type=house_type self.area=area self.free_area=area self.item=[] def __str__(self): return "户型: %s\n 总面积:%.2f [剩余:%.2f] \n家具: %s "% (self.house_type,self.area,self.free_area,self.item) def add_item(self,item): if item.area>self.free_area: print("房子装不下了") return self.item.append(item.name) self.free_area-=item.area class HouseItem: def __init__(self, name, area): self.name=name self.area=area def __str__(self): return "[%s]占地 %.2f" % (self.name,self.area) bed=HouseItem("席梦思",4) chest=HouseItem("衣柜",2) table=HouseItem("餐桌",60) my_house=House("两室一厅",60) my_house.add_item(bed) my_house.add_item(chest) my_house.add_item(table) print(my_house)
先创建两个类
1.房子类
2.家具类
然后在房子类里面添加家具类
标签:self div 子类 面向对象 name 面向 code __init__ use
原文地址:https://www.cnblogs.com/jestin/p/11969817.html