面向对象中的属性——方法变属性

为什么这里出错了?

img


img

img

@property  
def area(self):
    return self.width*self.height

多了一层缩进,成了 def init() 的子函数

class Rect():
    def __init__(self,width,height):
        self.width = width
        self.height = height

    # 这个要减少一层缩进放在def __init__函数之外
    @property
    def area(self):
        return self.width*self.height

rect = Rect(800,600)
print(rect.area)

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img