一文搞懂各种属性及方法

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)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值