存放家具
class Home:
def __init__(self,area):
#基本属性 面积 家具
self.area = area
self.furniture = []
def addfurniture(self,furniture):
#存放家具
if furniture.area > self.remnant():
print('空间不足,慎重考虑')
else:
self.furniture.append(furniture)
def remnant(self):
#剩余面积
num = self.area
for i in self.furniture:
num -= i.area
return num
def __str__(self):
#描述
info =('总面积:%s,剩余面积:%s,拥有家具:%s'%(self.area,self.remnant(),[f.name for f in self.furniture]))
return info
class Bed:
def __init__(self,name,area):
self.name = name
self.area = area
class Sofa:
def __init__(self,name,area):
self.name = name
self.area = area
class Closet:
def __init__(self,name,area):
self.name = name
self.area = area
home1 = Home(100)
print(home1)
bed = Bed('席梦思',5)
sofa = Sofa('沙发',10)
closet = Closet('衣柜',6)
home1.addfurniture(bed)
home1.addfurniture(sofa)
home1.addfurniture(closet)
print(home1)
print('==================================================================')
home2 = Home(100)
print(home2)
bed = Bed('席梦思',50)
sofa = Sofa('沙发',10)
closet = Closet('衣柜',60)
home2.addfurniture(bed)
home2.addfurniture(sofa)
home2.addfurniture(closet)
print(home2)