学习笔记,做一个添加家具的操作
学习笔记,做一个添加家具的操作
class JiaJu(): def __init__(self, name, area): self.name = name self.area = area def __str__(self): return ("[%s]的占地面积是%.2f平米" % (self.name, self.area)) class Horse(): def __init__(self, type, area): self.type = type self.area = area self.freearea = area self.jiaju_list = [] def __str__(self): return ("此房为%s\n面积是%.2f平米\n摆放的家具有%s\n剩余面积[%.2f平米]" % (self.type, self.area, self.jiaju_list, self.freearea)) def add_jiaju(self, item): print("要添加的家具是%s" % item) if item.area > self.freearea: print("没地方了") return self.freearea -= item.area self.jiaju_list.append(item.name) desk = JiaJu("课桌", 5) bed = JiaJu("床", 8) chair = JiaJu("椅子", 20) myhorse = Horse("LOFT", 30) myhorse.add_jiaju(desk) myhorse.add_jiaju(chair) myhorse.add_jiaju(bed) print(myhorse)