####################################################
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)
封裝、反射、裝飾器
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.