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é)
更多關于Python相關內(nèi)容感興趣的讀者可查看本站專題:《Python面向?qū)ο蟪绦蛟O計入門與進階教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設計有所幫助。
- python drf各類組件的用法和作用
- python定義類的簡單用法
- python db類用法說明
- Python面向?qū)ο蟪绦蛟O計之靜態(tài)方法、類方法、屬性方法原理與用法分析
- Python面向?qū)ο蟪绦蛟O計之類和對象、實例變量、類變量用法分析
- python編程進階之類和對象用法實例分析
- Python面向?qū)ο笾蓄悾╟lass)的簡單理解與用法分析
- Python3變量與基本數(shù)據(jù)類型用法實例分析
- python定義類self用法實例解析
- Python中類似于jquery的pyquery庫用法分析
- Python上下文管理器類和上下文管理器裝飾器contextmanager用法實例分析
- 詳細介紹python類及類的用法
相關文章
python如何實現(xiàn)質(zhì)數(shù)求和
這篇文章主要介紹了python如何實現(xiàn)質(zhì)數(shù)求和,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05
Python中xlsx文件轉(zhuǎn)置操作詳解(行轉(zhuǎn)列和列轉(zhuǎn)行)
很多時候我們處理的Excel表格并不是我們想要的樣子,需要將表格的形式進行相應轉(zhuǎn)換后進行數(shù)據(jù)分析操作,下面這篇文章主要給大家介紹了關于Python中xlsx文件轉(zhuǎn)置操作(行轉(zhuǎn)列和列轉(zhuǎn)行)的相關資料,需要的朋友可以參考下2022-07-07
python代碼實現(xiàn)學生信息管理系統(tǒng)
這篇文章主要為大家詳細介紹了python代碼實現(xiàn)學生信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05

