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