####################################################
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)
封装、反射、装饰器
最新推荐文章于 2023-03-27 16:25:30 发布