一文搞懂各种属性及方法

烈酒焚心 提交于 2020-01-17 17:06:51
class Base:
    def __init__(self):
        self.__x = "a"
    __private = 1024
    count = 100
    @classmethod
    def rcount(cls):
        print(Base.count)

    @staticmethod
    def sta_rcount():
        print(Base.count + 1)

    def free():
        print("freedom")


class Derived(Base):
    def test(self):
        '''
        测试私有类属性、私有实例属性可访问性
        :return:
        '''
        print(Base._Base__private)
        print(Derived._Base__private)
        print(self._Base__x)
    pass


b = Base()
dc1 = Derived()
# 类属性
print(b.count, dc1.count, Base.count, Derived.count)
# 类方法
dc1.rcount()
b.rcount()
Base.rcount()
Derived.rcount()
# 静态方法
dc1.sta_rcount()
b.sta_rcount()
Base.sta_rcount()
Derived.sta_rcount()
# 自由方法
Base.free()
Derived.free()

# 私有类属性
print(Base._Base__private, Derived._Base__private, b._Base__private, dc1._Base__private)

dc1.test()
#私有实例属性
print(b._Base__x, dc1._Base__x)

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!