Python 面向?qū)ο笾恈lass和對象基本用法示例
本文實例講述了Python 面向?qū)ο笾恈lass和對象基本用法。分享給大家供大家參考,具體如下:
類(class):定義一件事物的抽象特點,usually,類定義了事物的屬性和它可以做到的性為
對象(object):是類的實例。
1.基本點
class MyClass(object): message = "hello,world" def show(self): print (self.message)
類名為MyClass 有一個成員變量:message,并賦予初值
類中定義了成員函數(shù)show(self),注意類中的成員函數(shù)必須帶有參數(shù)self
參數(shù)self是對象本身的引用,在成員函數(shù)中可以引用self參數(shù)獲得對象的信息
輸出結(jié)果:
inst = Myclass() # 實例化一個MyClass 的對象 inst.show # 調(diào)用成員函數(shù),無需傳入self參數(shù) hello,world
注: 通過在類名后面加小括號可以直接實例化類來獲得對象變量,使用對象變量可以訪問類的成員函數(shù)與成員變量。
2.構(gòu)造函數(shù)
構(gòu)造函數(shù)是一種特殊的類成員方法,主要用來創(chuàng)建對象初始化,python 中的類構(gòu)造函數(shù)用__init__命名:
class MyClass(object): message = 'Hello, Developer.' def show(self): print self.message def __init__(self): print "Constructor is called" inst = MyClass() inst.show() >>>
打印結(jié)果:
>>>Constructor is called >>>Hello, Developer.
注:構(gòu)造函數(shù)不能有返回值,python 中不能定義多個構(gòu)造函數(shù),但可以通過為命名參數(shù)提供默認值的方式達到用多種方式構(gòu)造對象的目的。
3.析構(gòu)函數(shù)
是構(gòu)造的反向函數(shù),在銷毀或者釋放對象時調(diào)用他們。
python 中為類定義析構(gòu)函數(shù)的方法在類定義中定義一個名為__del__的沒有返回值和參數(shù)的函數(shù)。
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 >>>
打印結(jié)果:
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.實例成員變量
構(gòu)造函數(shù)中定義self引用的變量,因此這樣的成員變量在python中叫做實例成員變量。
def __init__(self, name = "unset", color = "black"): print "Constructor is called with params: ",name, " ", color self.name = name self.color = color
5.靜態(tài)函數(shù)和類函數(shù):
python 支持兩種基于類名訪問成員的函數(shù):靜態(tài)函數(shù),類函數(shù)。
區(qū)別在于:類函數(shù)有一個隱形參數(shù)cls可以用來獲取類信息。而靜態(tài)函數(shù)沒有該函數(shù)。
靜態(tài)函數(shù)用裝飾器:@staticmethod定義
類函數(shù)使用裝飾器:@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, name, color): print ("Object will be created: %s(%s, %s)"% (cls.__name__, name, color)) return cls(name, color) def __init__(self, name = "unset", color = "black"): print ("Constructor is called with params: ",name, " ", 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
輸出結(jié)果:
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, name = "unset", color = "black"): print "Constructor is called with params: ",name, " ", color self.__name = name self.__color = color def __del__(self): print "Destructor is called for %s!"% self.__name inst = MyClass("Jojo", "White") del inst
輸出結(jié)果:
Constructor is called with params: Jojo White
Destructor is called for Jojo!
注明:書《Python 高效開發(fā)實戰(zhàn)Django, Tornado, Flask, Twisted》總結(jié)
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python面向?qū)ο蟪绦蛟O(shè)計入門與進階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
- python drf各類組件的用法和作用
- python定義類的簡單用法
- python db類用法說明
- Python面向?qū)ο蟪绦蛟O(shè)計之靜態(tài)方法、類方法、屬性方法原理與用法分析
- Python面向?qū)ο蟪绦蛟O(shè)計之類和對象、實例變量、類變量用法分析
- python編程進階之類和對象用法實例分析
- Python面向?qū)ο笾蓄悾╟lass)的簡單理解與用法分析
- Python3變量與基本數(shù)據(jù)類型用法實例分析
- python定義類self用法實例解析
- Python中類似于jquery的pyquery庫用法分析
- Python上下文管理器類和上下文管理器裝飾器contextmanager用法實例分析
- 詳細介紹python類及類的用法
相關(guān)文章
詳解python中@classmethod和@staticmethod方法
在python類當(dāng)中,經(jīng)常會遇到@classmethod和@staticmethod這兩個裝飾器,那么到底它們的區(qū)別和作用是啥子呢?本文結(jié)合場景分析給大家詳細講解,感興趣的朋友跟隨小編一起看看吧2022-10-10妙用itchat! python實現(xiàn)久坐提醒功能
python編寫的久坐提醒,給最愛的那個她,這篇文章主要為大家分享了python久坐提醒功能的實現(xiàn)代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11通過數(shù)據(jù)庫向Django模型添加字段的示例
這篇文章主要介紹了通過數(shù)據(jù)庫向Django模型添加字段的示例,Django是人氣最高的Python web開發(fā)框架,需要的朋友可以參考下2015-07-07Python使用當(dāng)前時間、隨機數(shù)產(chǎn)生一個唯一數(shù)字的方法
這篇文章主要介紹了Python使用當(dāng)前時間、隨機數(shù)產(chǎn)生一個唯一數(shù)字的方法,涉及Python時間與隨機數(shù)相關(guān)操作技巧,需要的朋友可以參考下2017-09-09python 通過logging寫入日志到文件和控制臺的實例
下面小編就為大家分享一篇python 通過logging寫入日志到文件和控制臺的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04Keras 利用sklearn的ROC-AUC建立評價函數(shù)詳解
這篇文章主要介紹了Keras 利用sklearn的ROC-AUC建立評價函數(shù)詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06