14.6.参考案例:存放家具
class Home():
def __init__(self, area):
self.area = area
self.containsItem = []
def __str__(self):
msg = "当前房间可用面积为:" + str(self.area)
if len(self.containsItem) > 0:
msg = msg + " 容纳的物品有: "
for temp in self.containsItem:
msg = msg + temp.getName() + ", "
msg = msg.strip(", ")
return msg
def accommodateItem(self,item):
needArea = item.getUsedArea()
if self.area > needArea:
self.containsItem.append(item)
self.area -= needArea
print("ok:已经存放到房间中")
else: print("err:房间可用面积为:%d,但是当前要存放的物品需要的面积为%d"%(self.area, needArea))
class Bed:
def __init__(self,area,name = '床'):
self.name = name
self.area = area
def __str__(self):
msg = '床的面积为:' + str(self.area)
return msg
def getUsedArea(self):
return self.area
def getName(self):
return self.name
newHome = Home(100)
print(newHome)
newBed = Bed(20)
print(newBed)
newHome.accommodateItem(newBed)
print(newHome)
newBed2 = Bed(30,'席梦思')
print(newBed2)
newHome.accommodateItem(newBed2)
print(newHome)
总结:
- 如果一个对象与另外一个对象有一定的关系,那么一个对象可用是另外一个对象的属性
思维升华: