求大神解答!!!
class HouseItem:def __init__(self,name,daxiao):
self.name = name
self.daxiao = daxiao
def __str__(self):
return f"家具名称为:{self.name},占地大小为:{self.daxiao}"
class House:
def __init__(self,hx,zmj):
self.hx = hx
self.zmj = zmj
self.syu = zmj
fjsy_list = []
def __str__(self):
return f"房子的户型是{self.hx},房子的面积是:{self.zmj},剩余面积是:{self.syu}"
def jiaju(self,mile):
print(f"您已添加{mile}")
chuang = HouseItem("席梦思",4)
yigui = HouseItem("柜子",2)
changzuo = HouseItem("餐桌",1.5)
print(f"{chuang}\n{yigui}\n{changzuo}")
kai = House("两室一厅",60)
House.jiaju(chuang)
print(kai) 没有传递参数 mile,加上就行了
class HouseItem:
def __init__(self,name,daxiao):
self.name = name
self.daxiao = daxiao
def __str__(self):
return f"家具名称为:{self.name},占地大小为:{self.daxiao}"
class House:
def __init__(self,hx,zmj):
self.hx = hx
self.zmj = zmj
self.syu = zmj
fjsy_list = []
def __str__(self):
return f"房子的户型是{self.hx},房子的面积是:{self.zmj},剩余面积是:{self.syu}"
def jiaju(self,mile):
print(f"您已添加{mile}")
chuang = HouseItem("席梦思",4)
yigui = HouseItem("柜子",2)
changzuo = HouseItem("餐桌",1.5)
print(f"{chuang}\n{yigui}\n{changzuo}")
kai = House("两室一厅",60)
House.jiaju(chuang, 1.2)
print(kai)
错误原因
没有对类进行实例化。
错误的地方帮你标注上了,如下:
class HouseItem:
def __init__(self,name,daxiao):
self.name = name
self.daxiao = daxiao
def __str__(self):
return f"家具名称为:{self.name},占地大小为:{self.daxiao}"
class House:
def __init__(self,hx,zmj):
self.hx = hx
self.zmj = zmj
self.syu = zmj
fjsy_list = []
def __str__(self):
return f"房子的户型是{self.hx},房子的面积是:{self.zmj},剩余面积是:{self.syu}"
def jiaju(self,mile):
# 没有减少面积的运算
self.syu -= mile.daxiao
print(f"您已添加{mile}")
chuang = HouseItem("席梦思",4)
yigui = HouseItem("柜子",2)
changzuo = HouseItem("餐桌",1.5)
print(f"{chuang}\n{yigui}\n{changzuo}")
kai = House("两室一厅",60)
# House.jiaju(chuang)
kai.jiaju(chuang)
print(kai)
页:
[1]