新聞中心
本篇文章為大家展示了Python中class和對(duì)象基本用法是什么,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
成都創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)墨玉,十年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):028-86922220
主要介紹了Python 面向?qū)ο笾恈lass和對(duì)象基本用法,結(jié)合實(shí)例形式詳細(xì)分析了Python面向?qū)ο蟪绦蛟O(shè)計(jì)中類class和對(duì)象基本概念、原理、使用方法與操作注意事項(xiàng),需要的朋友可以參考下。
類(class):定義一件事物的抽象特點(diǎn),usually,類定義了事物的屬性和它可以做到的性為對(duì)象(object):是類的實(shí)例。
1.基本點(diǎn)
< code class="prism language-bash">class MyClass(object):
message = "hello,world"
def show(self):
print (self.message)
< /code>
類名為MyClass 有一個(gè)成員變量:message,并賦予初值。
類中定義了成員函數(shù)show(self),注意類中的成員函數(shù)必須帶有參數(shù)self。
參數(shù)self是對(duì)象本身的引用,在成員函數(shù)中可以引用self參數(shù)獲得對(duì)象的信息。
輸出結(jié)果:
< code class="prism language-bash">inst = Myclass() # 實(shí)例化一個(gè)MyClass 的對(duì)象
inst.show # 調(diào)用成員函數(shù),無(wú)需傳入self參數(shù)
hello,world
< /code>
注: 通過(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__命名:
< code class="prism language-bash">class MyClass(object):
message = 'Hello, Developer.'
def show(self):
print self.message
def __init__(self):
print "Constructor is called"
inst = MyClass()
inst.show()
>>>
< /code>
打印結(jié)果:
< code class="prism language-bash">>>>Constructor is called
>>>Hello, Developer.
< /code>
注:構(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è)名為_(kāi)_del__的沒(méi)有返回值和參數(shù)的函數(shù)。
< code class="prism language-bash">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
>>>
< /code>
4.實(shí)例成員變量
構(gòu)造函數(shù)中定義self引用的變量,因此這樣的成員變量在python中叫做實(shí)例成員變量。
< code class="prism language-bash">def __init__(self, name = "unset", color = "black"):
print "Constructor is called with params: ",name, " ", color
self.name = name
self.color = color
< /code>
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定義
< code class="prism language-bash">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
< /code>
6.私有成員
python 使用指定變量名格式的方法定義私有成員,即所有以雙下劃線“__”開(kāi)始命名的成員都為私有成員。
< code class="prism language-bash">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
< /code>
上述內(nèi)容就是Python中class和對(duì)象基本用法是什么,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文題目:Python中class和對(duì)象基本用法是什么
分享網(wǎng)址:http://www.ef60e0e.cn/article/gcosgs.html