python3之面向对象实例存家具
#定义一个房子类以及一个家具类,各自定义属性,实现两个类的关联及调用
class Home:
def __init__(self,area,info,addr):
self.area = area #面积
self.info = info #户型
self.addr = addr #位置
self.leftarea = area #剩余面积初始值为全面积
self.add_jiaju = []
def __str__(self):
return "房子面积是%s,户型是%s,位置是%s,老杨新增了家具%s,剩余套内面积%s"%(self.area,self.info,self.addr,str(self.add_jiaju),self.leftarea)
def add_items(self,items):
# self.leftarea -= items.area
# self.add_jiaju.append(items.name)
self.leftarea -= items.get_area()#直接调用其他类属性改为调函数,后期可控制返回值
self.add_jiaju.append(items.get_name())
class jaiju:
def __init__(self,name,area):
self.name = name
self.area = area
def __str__(self):
return "%s的面积是%s"%(self.name,self.area)
def get_area(self):
return self.area
def get_name(self):
return self.name
home1 = Home(115,"三室两厅","河南郑州")
bed1 = jaiju("沙发",5)
home1.add_items(bed1)
shafa = jaiju("茶几",4)
home1.add_items(shafa)
yizi = jaiju("椅子",2)
home1.add_items(yizi)
print(home1)
声明:师从老男孩太白金星,不对代码做任何保证,如有问题请自携兵刃直奔沙河