封装、反射、装饰器

####################################################
class foo(object):
    def __init__(self):
        self.func()
    def func(self):
        print('a')
class son(foo):
    def func(self):
        print('b')
son()

class foo(object):
    def __init__(self):
        self.__func()
    def __func(self):
        print('a')
class son(foo):
    def __func(self):      ##会从父类调用__init__
        print('b')

son()

class foo(object):
    def __func(self):
        print('a')
class son(foo):
    def __init__(self):
        self.__func()      #会报错,私有的内容不能被子类继承
# son()

#property的使用场景1:和私有的属性合作,可以查看数据不能修改
class User():
    def __init__(self,user,pwd):
        self.user = user
        self.__pwd = pwd

    @property
    def pwd(self):
        return self.__pwd

alex = User('alex','av')
print(alex.pwd)    

####################################################################
#反射
#
class Test_file():
    l = [('读文件','read'),('写文件','write'),('复制文件','copy'),("移动文件",'move')]
    def read(self):
        print('in read file')
    def write(self):
        print('in write file')
    def copy(self):
        print('in copy file')
    def move(self):
        print('in move file')
f = Test_file()
while True:
    for index, opj in enumerate(f.l, 1):
        print(index, opj[0])
    i = int(input("请输入您要操作的序号:"))
    if hasattr(f,opj[i]):
        getattr(f,opj[i])()



###########################
# @classmethod  装饰器
class goods():
    __discount = 0.8
    def __init__(self):
        self.__price = 5
        self.price = self.__price * self.__discount
    def change_discount(self,new_discount):
        goods.__discount = new_discount

# c = goods()
# print(c.price)

class goods():
    __discount = 0.8
    def __init__(self):
        self.__price = 5
        self.price = self.__price * self.__discount
    @classmethod   #把一个对象绑定的方法,修改成一个类方法
    def change_discount(cls,new_discount):
        cls.__discount = new_discount

c = goods()
print(c.price)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值