学习笔记(10):21天通关Python(仅视频课)-类变量与实例变量

立即学习:https://edu.csdn.net/course/play/24797/282190?utm_source=blogtoedu

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

    def getmianji(self):
        print(self.width * self.height)

    def setdaxiao(self, size):
        self.width = size[0]
        self.height = size[1]

    def getdaxiao(self):
        return self.width, self.height

    mianji = property(fget=getmianji, doc='获得面积')
    daxiao = property(fget=getdaxiao, fset=setdaxiao, doc='获得大小')


tp = TestProprty(3, 4)
tp.mianji
print(tp.daxiao)
tp.daxiao = (7, 8)
tp.mianji
print('_' * 50)


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

    @property
    def mianji(self):
        print(self.width * self.height)

    @property
    def daxiao(self):
        return self.width, self.height

    @daxiao.setter
    def daxiao(self, size):
        self.width = size[0]
        self.height = size[1]


tp = TestProprty2(4, 6)
tp.mianji
print(tp.daxiao)
tp.daxiao = (6, 7)
tp.mianji
发布了25 篇原创文章 · 获赞 4 · 访问量 606

猜你喜欢

转载自blog.csdn.net/happyk213/article/details/105145460