Python面向?qū)ο蟪绦蛟O(shè)計(jì)構(gòu)造函數(shù)和析構(gòu)函數(shù)用法分析
本文實(shí)例講述了Python面向?qū)ο蟪绦蛟O(shè)計(jì)構(gòu)造函數(shù)和析構(gòu)函數(shù)用法。分享給大家供大家參考,具體如下:
構(gòu)造函數(shù)和析構(gòu)函數(shù)
1、構(gòu)造方法的使用
很多類都傾向于將對(duì)象創(chuàng)建為有初始化狀態(tài).因此類可以定義一個(gè)名為init()的特殊方法(構(gòu)造方法)來實(shí)例化一個(gè)對(duì)象。
構(gòu)造方法也叫做構(gòu)造器,是指當(dāng)實(shí)例化一個(gè)對(duì)象(創(chuàng)建一個(gè)對(duì)象)的時(shí)候,第一個(gè)被自動(dòng)調(diào)用的方法。
演示1:構(gòu)造方法被調(diào)用的契機(jī)
class Person():
#構(gòu)造方法
def __init__(self):
print("構(gòu)造函數(shù)被執(zhí)行了")
#創(chuàng)建對(duì)象的過程中構(gòu)造函數(shù)被自動(dòng)調(diào)用
p1 = Person()
輸出:
構(gòu)造函數(shù)被執(zhí)行了
結(jié)論:創(chuàng)建對(duì)象的過程中調(diào)用了構(gòu)造函數(shù)。
當(dāng)未手動(dòng)添加構(gòu)造函數(shù)時(shí),系統(tǒng)會(huì)默認(rèn)提供一個(gè)無參的構(gòu)造函數(shù)。
演示2:構(gòu)造函數(shù)和普通函數(shù)之間的區(qū)別
說明:構(gòu)造函數(shù)本質(zhì)上還是一個(gè)函數(shù),函數(shù)可以有參數(shù),也可以無參,所以同樣的道理,構(gòu)造函數(shù)也是如此。
class Person():
#一般情況下,構(gòu)造方法的參數(shù)和成員變量有關(guān),并且在設(shè)置的過程中與成員變量同名
def __init__(self, name, age, height, weight):
#print(nname, age, height, weight)
#因?yàn)闃?gòu)造方法是創(chuàng)建對(duì)象的過程中被調(diào)用的
#所以構(gòu)造方法的作用一般是用來定義成員變量并且給成員變量賦值
#定義屬性并給屬性賦值
#通過self來進(jìn)行區(qū)分是成員變量還是形參
self.name = name
self.age = age
self.height = height
self.weight = weight
def run(self):
print("run")
def eat(self, food):
print("eat "+ food)
'''
構(gòu)造函數(shù): __init__() 在使用類創(chuàng)建對(duì)象的時(shí)候自動(dòng)調(diào)用
注意: 如果不顯式的寫出構(gòu)造函數(shù),默認(rèn)會(huì)自動(dòng)添加一個(gè)空的構(gòu)造函數(shù),函數(shù)體中什么都不實(shí)現(xiàn)
'''
per = Person("hanmeimei", 20, 170, 55)
print(per.name, per.age)
per.run()
per2 = Person("lili", 21, 175, 70)
print(per2.name, per2.age)
輸出:
hanmeimei 20
run
lilei 21
2、self的使用
注意:self代表類的實(shí)例[對(duì)象],而非類本身。
類的方法與普通的函數(shù)只有一個(gè)特殊的區(qū)別—>他們必須有一個(gè)額外的第一個(gè)參數(shù)名稱,按照慣例它的名字是self。
self.__class__:返回的是當(dāng)前類的類名
class Test():
def prt(self):
print(self)
print(self.__class__)
t = Test()
t.prt()
輸出:
<__main__.Test object at 0x0000017D47D81B00>
<class '__main__.Test'>
演示2:self不是python的關(guān)鍵字
class Person():
def run(self):
print("run")
print(self.__class__)
p = self.__class__("tt", 30, 10, 30)
def eat(self,food):
print("eat" + food)
def say(self):
print("hello, my name is %s, I am %d year old" % (self.name, self.age))
def play(a):
print("play", a.name)
def __init__(self, name, age, height, weight):
self.name = name
self.age = age
self.height = height
self.weight = weight
# self代表此時(shí)正在創(chuàng)建對(duì)象,self.屬性表示當(dāng)前對(duì)象的屬性
per1 = Person("tom", 20, 160, 80)
per1.say()
per2 = Person("henmeimei", 21, 160, 80)
per2.say()
per1.say()
per1.run()
輸出:
hello, my name is tom, I am 20 year old
hello, my name is henmeimei, I am 21 year old
hello, my name is tom, I am 20 year old
run
<class '__main__.Person'>
3、析構(gòu)函數(shù)的使用
析構(gòu)函數(shù)調(diào)用的契機(jī)【對(duì)象被銷毀的時(shí)候】:
1、程序執(zhí)行結(jié)束,會(huì)自動(dòng)調(diào)用析構(gòu)函數(shù)
2、使用del 刪除對(duì)象的時(shí)候,系統(tǒng)會(huì)自動(dòng)調(diào)用析構(gòu)函數(shù)
注意:如果沒寫析構(gòu)函數(shù),當(dāng)符合析構(gòu)函數(shù)調(diào)用的契機(jī)時(shí),系統(tǒng)會(huì)自動(dòng)調(diào)用父類的析構(gòu)函數(shù)。
演示:
class Person():
def run(self):
print("run")
def eat(self, food):
print("eat "+food)
def __init__(self, name, age, height, weight):
self.name = name
self.age = age
self.height = height
self.weight = weight
def __del__(self):
print("析構(gòu)函數(shù)被調(diào)用了")
per = Person("hanmeimei", 20, 170, 55)
#釋放對(duì)象
del per
#注意:對(duì)象釋放后就不能再進(jìn)行訪問了
#在函數(shù)里定義的對(duì)象,會(huì)在函數(shù)結(jié)束時(shí)自動(dòng)釋放,這樣可以用來減少內(nèi)存空間的浪費(fèi)
#其實(shí)就是作用域的問題
def func():
per2 = Person("aa", 1, 1, 1)
func()
輸出:
析構(gòu)函數(shù)被調(diào)用了
析構(gòu)函數(shù)被調(diào)用了
class Animal(object):
def __del__(self):
print('Animal父類的析構(gòu)函數(shù)被調(diào)用了')
class Cat(Animal):
pass
cat = Cat()
輸出:
Animal父類的析構(gòu)函數(shù)被調(diào)用了
class A():
def __init__(self):
print("父類A構(gòu)造函數(shù)被調(diào)用了")
def __del__(self):
print("父類A析構(gòu)函數(shù)被調(diào)用了")
class B(A):
def __init__(self):
print("子類B構(gòu)造函數(shù)被調(diào)用了")
A.__init__(self)
def __del__(self):
print("子類B析構(gòu)函數(shù)被調(diào)用了")
class C(A):
pass
def main():
b = B()
del b
print("*********************")
c = C()
if __name__ == '__main__':
main()
輸出:
子類B構(gòu)造函數(shù)被調(diào)用了
父類A構(gòu)造函數(shù)被調(diào)用了
子類B析構(gòu)函數(shù)被調(diào)用了
*********************
父類A構(gòu)造函數(shù)被調(diào)用了
父類A析構(gòu)函數(shù)被調(diào)用了
更多關(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ì)有所幫助。
相關(guān)文章
Python批量生成特定尺寸圖片及圖畫任意文字的實(shí)例
今天小編就為大家分享一篇Python批量生成特定尺寸圖片及圖畫任意文字的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01
Python實(shí)現(xiàn)PS濾鏡中馬賽克效果示例
這篇文章主要介紹了Python實(shí)現(xiàn)PS濾鏡中馬賽克效果,涉及Python基于skimage庫(kù)的圖形馬賽克效果相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-01-01
Python調(diào)用AnythingLLM API使用流輸出的實(shí)現(xiàn)
本文主要介紹了Python調(diào)用AnythingLLM API使用流輸出的實(shí)現(xiàn),用于處理長(zhǎng)文本或?qū)崟r(shí)交互場(chǎng)景,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03
python 數(shù)據(jù)生成excel導(dǎo)出(xlwt,wlsxwrite)代碼實(shí)例
這篇文章主要介紹了python 數(shù)據(jù)生成excel導(dǎo)出(xlwt,wlsxwrite)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
PyQt 實(shí)現(xiàn)使窗口中的元素跟隨窗口大小的變化而變化
今天小編就為大家分享一篇PyQt 實(shí)現(xiàn)使窗口中的元素跟隨窗口大小的變化而變化,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06
Python內(nèi)建類型float源碼學(xué)習(xí)
這篇文章主要為大家介紹了Python內(nèi)建類型float源碼學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
python函數(shù)也可以是一個(gè)對(duì)象,可以存放在列表中并調(diào)用方式
這篇文章主要介紹了python函數(shù)也可以是一個(gè)對(duì)象,可以存放在列表中并調(diào)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02

