1. 程式人生 > >面向對象的封裝2

面向對象的封裝2

miss tin body let val set dal () typeerror

class Foo:
    def __func(self): #私有屬性在定義階段就發生變化,_Foo__func
        print(Foo)
class  Bar(Foo):
    def __func(self): #因為在定義階段就發生了變化,所以子類不能繼承父類的私有屬性
        print(Bar)


b = Bar()
b._Foo__func()
b._Bar__func()

class A:
    def foo(self):
        print(A.foo)
        self.__bar() #調用私有方法,找不到,除了自己類以外的方法
def __bar(self): print(A.bar) class B(A): def __bar(self): print(B.bar) b = B() b.foo() class Teacher(): def __init__(self,username,password): if not isinstance(password,str): raise Exception(密碼只能是字符串) self.__uname = username self.
__pwd = password @property def name(self): return self.__uname @name.setter def name(self,val): print(type(val)) if not isinstance(val,str): raise TypeError(must be str) self.__uname=val @name.deleter def name(self): import
re if re.findall((?i)sb,self.__uname): #(?i) 不區分大小寫 raise PermissionError(SB not Deleting permissions) else: del self.__uname egon = Teacher(egon,123) egon.name = (egonSB) print(egon.name) del egon.name

面向對象的封裝2