定義了一個Animal類,該類包括了構造函數,私有方法,公有方法,靜態方法,屬性的方問等
雙下劃線"__"組成了私有成員的定義約束,其它情況則為公有成員
#_metaclass_=type # 確定使用新式類
class Animal:
address="acccd";
def __init__(self): #構造方法 一個對象創建后會立即調用此方法
self.Name="Doraemon"
PRint(self.Name);
def accessibleMethod(self): #綁定方法 對外公開
print (self.Name);
self.__inaccessible()
def __inaccessible(self): #私有方法 對外不公開 以雙下劃線開頭
print( "U cannot see me...");
@staticmethod
def staticMethod():
#self.accessibleMethod() #在靜態方法中無法直接調用實例方法 直接拋出異常
print("this is a static method");
def setName(self,name): #訪問器函數
self.Name=name
def getName(self): #訪問器函數
return self.Name
#name=property(getName,setName) #屬性 可讀可寫
Animal.staticMethod();
t=Animal();
t.setName("ddd");
print(t.getName());
print(getattr(t,"address"));
setattr(t,"address","fjfkejkj");
print(getattr(t,"address"));
類自身的一些屬性成員,包括類的名稱字符串,繼承等
print("Animal.__name__:", Animal.__name__);
print("Animal.__module__:", Animal.__module__);
print( "Animal.__bases__:", Animal.__bases__);
print("Animal.__dict__:", Animal.__dict__);
新聞熱點
疑難解答