Python 面向物件之類class和物件基本用法示例
本文例項講述了Python 面向物件之類class和物件基本用法。分享給大家供大家參考,具體如下:
類(class):定義一件事物的抽象特點,usually,類定義了事物的屬性和它可以做到的性為
物件(object):是類的例項。
1.基本點
class MyClass(object): message = "hello,world" def show(self): print (self.message)
類名為MyClass 有一個成員變數:message,並賦予初值
類中定義了成員函式show(self),注意類中的成員函式必須帶有引數self
引數self是物件本身的引用,在成員函式中可以引用self引數獲得物件的資訊
輸出結果:
inst = Myclass() # 例項化一個MyClass 的物件 inst.show # 呼叫成員函式,無需傳入self引數 hello,world
注: 通過在類名後面加小括號可以直接例項化類來獲得物件變數,使用物件變數可以訪問類的成員函式與成員變數。
2.建構函式
建構函式是一種特殊的類成員方法,主要用來建立物件初始化,python 中的類建構函式用__init__命名:
class MyClass(object): message = 'Hello,Developer.' def show(self): print self.message def __init__(self): print "Constructor is called" inst = MyClass() inst.show() >>>
列印結果:
>>>Constructor is called >>>Hello,Developer.
注:建構函式不能有返回值,python 中不能定義多個建構函式,但可以通過為命名引數提供預設值的方式達到用多種方式構造物件的目的。
3.解構函式
是構造的反向函式,在銷燬或者釋放物件時呼叫他們。
python 中為類定義解構函式的方法在類定義中定義一個名為__del__的沒有返回值和引數的函式。
class MyClass(object): message = 'Hello,Developer.' def show(self): print self.message def __init__(self,name = "unset",color = "black"): print "Constructor is called with params: ",name," ",color def __del__(self): print "Destructor is called!" inst = MyClass() inst.show() inst2 = MyClass("David") inst2.show() del inst,inst2 inst3 = MyClass("Lisa","Yellow") inst3.show() del inst3 >>>
列印結果:
Constructor is called with params: unset black
Hello,Developer.
Constructor is called with params: David black
Hello,Developer.
Destructor is called!
Destructor is called!
Constructor is called with params: Lisa Yellow
Hello,Developer.
Destructor is called!
4.例項成員變數
建構函式中定義self引用的變數,因此這樣的成員變數在python中叫做例項成員變數。
def __init__(self,color = "black"): print "Constructor is called with params: ",color self.name = name self.color = color
5.靜態函式和類函式:
python 支援兩種基於類名訪問成員的函式:靜態函式,類函式。
區別在於:類函式有一個隱形引數cls可以用來獲取類資訊。而靜態函式沒有該函式。
靜態函式用裝飾器:@staticmethod定義
類函式使用裝飾器:@classmethod定義
class MyClass(object): message = 'Hello,Developer.' def show(self): print (self.message) print ("Here is %s in %s!" % (self.name,self.color)) @staticmethod def printMessage(): print ("printMessage is called") print (MyClass.message) @classmethod def createObj(cls,color): print ("Object will be created: %s(%s,%s)"% (cls.__name__,color)) return cls(name,color) def __init__(self,color = "black"): print ("Constructor is called with params: ",color) self.name = name self.color = color def __del__(self): print ("Destructor is called for %s!"% self.name) MyClass.printMessage() inst = MyClass.createObj( "Toby","Red") print (inst.message) del inst
輸出結果:
printMessage is called
Hello,Developer.
Object will be created: MyClass(Toby,Red)
Constructor is called with params: Toby Red
Hello,Developer.
Destructor is called for Toby!
6.私有成員
python 使用指定變數名格式的方法定義私有成員,即所有以雙下劃線“__”開始命名的成員都為私有成員。
class MyClass(object): def __init__(self,color self.__name = name self.__color = color def __del__(self): print "Destructor is called for %s!"% self.__name inst = MyClass("Jojo","White") del inst
輸出結果:
Constructor is called with params: Jojo White
Destructor is called for Jojo!
註明:書《Python 高效開發實戰Django,Tornado,Flask,Twisted》總結
更多關於Python相關內容感興趣的讀者可檢視本站專題:《Python面向物件程式設計入門與進階教程》、《Python資料結構與演算法教程》、《Python函式使用技巧總結》、《Python字串操作技巧彙總》、《Python編碼操作技巧總結》及《Python入門與進階經典教程》
希望本文所述對大家Python程式設計有所幫助。