要總結(jié)的概念
1.什么是繼承?
舉個(gè)小案例:
1 class Father():
2 def __init__(self,name):
3 self.name=name
4 self.property=3
5 def __str__(self):
6 return "%s喜歡python有%d種特長(zhǎng)"%(self.name,self.property)
7 son=Father("小明")
8 print(son)
在這里面son繼承的是一個(gè)叫Father的類的實(shí)例屬性:喜歡Python有3鐘特長(zhǎng)。說的簡(jiǎn)單的是先定義一個(gè)類Father,然后創(chuàng)建一個(gè)實(shí)例對(duì)象son,son便可以繼承Father的實(shí)例屬性
2.什么叫多態(tài)?
舉個(gè)案例如下:
1 class Father():
2 def __init__(self):
3 self.property=3
4 def __str__(self):
5 return "有%d種特長(zhǎng)"%(self.property)
6 def python(self):
7 print("我是python全棧工程師")
8 class Mother():
9 def __init__(self):
10 self.skills=3
11 def __str__(self):
12 return "我會(huì)%d種專業(yè)技能"%self.skills
13 def sing(self):
14 print("我會(huì)唱歌")
15
16 class Son(Father,Mother):
17 def java(self):
18 print("我對(duì)編程感興趣")
19 xiaoming=Son()
20 xiaoming.python()
21 xiaoming.java()
22 print(xiaoming)
在這里我們定義了三個(gè)類:Father,Mother,Son ,創(chuàng)建一個(gè)實(shí)例對(duì)象為小明,小明為son類,既可以調(diào)用Father類的實(shí)例屬性也可以調(diào)用Mother的實(shí)例屬性,當(dāng)然換可以重寫專屬于自己的屬性和方法,其實(shí)多態(tài)就是更容易編寫出出通用的代碼,做出通用的編程,通過基礎(chǔ)和重寫以適應(yīng)需求的不斷變化!
3.類與對(duì)象
類:具有相同屬性和行為的事物的總稱
對(duì)象:就是類的具體實(shí)例
類和對(duì)象:類是對(duì)象的模子,對(duì)象是類的實(shí)例
4.什么叫重寫?
舉個(gè)例子:
1 class Father():
2 def __init__(self):
3 self.property=3
4 def __str__(self):
5 return "有%d種特長(zhǎng)"%(self.property)
6 def python(self):
7 print("我是python全棧工程師")
8 class Mother():
9 def __init__(self):
10 self.skills=3
11 def __str__(self):
12 return "我會(huì)%d種專業(yè)技能"%self.skills
13 def sing(self):
14 print("我會(huì)唱歌")
15
16 class Son(Father,Mother):
17 def program(self):
18 print("我對(duì)編程感興趣")
19 def python(self):
20 print("我會(huì)python和java")
21 xiaoming=Son()
22 xiaoming.python()
23 xiaoming.sing()
24 xiaoming.program()
25 print(xiaoming)
Father的實(shí)例方法Python和Son的實(shí)例方法Python的實(shí)例方法雖然是一樣的,但小明的調(diào)用的Python的卻是Son的Python的方法,F(xiàn)ather 的Python的方法被Son里的Python重寫了。
4.什么是私有屬性?
在屬性前加兩個(gè)下劃線變?yōu)樗接袑傩?
5.什么是私有方法?
在方法前加兩個(gè)下劃線變?yōu)樗接蟹椒?
6.多繼承的注意點(diǎn)
在多繼承中如果出現(xiàn)相同的調(diào)用方法,那么只能調(diào)用一個(gè),并且按繼承順序調(diào)用
7.如何調(diào)用父類方法?
舉個(gè)例子
1 class Father():
2 instance=8
3 def __init__(self):
4 self.skills=6
5 print("father的方法")
6 def __str__(self):
7 return "我會(huì)的技能有%d種"%self.skills
8 @classmethod
9 def song(cls):
10 print("唱歌")
11 @staticmethod
12 def directory():
13 print("打印目錄")
14 class Son(Father):
15 def __init__(self):
16 super().__init__()
17 Father.__init__(self)
18 def operate(self):
19 print("我的操作猛如虎")
20 xiaoming=Son()
Son類調(diào)用Father的__init__()實(shí)例方法有兩種調(diào)用:1.super().__init__()
2.Father.__init__(self)
8.類方法寫法,類屬性寫法,靜態(tài)方法寫法,魔法方法以及它們?nèi)绾握{(diào)用?
舉個(gè)例子:
1 class Father(object):
2 instance=8
3 def __new__(cls):
4 print("要返回了...")
5 return object.__new__(cls)
6 print("第一次返回")
7 return super().__new__(cls)
8 print("第二次返回")
9 def __init__(self):
10 self.skills=6
11 print("father的方法")
12 def __str__(self):
13 return "我會(huì)的技能有%d種"%self.skills
14 def __del__(self):
15 print("del功能要執(zhí)行了.....")
16 @classmethod
17 def song(cls):
18 print("唱歌")
19 @staticmethod
20 def directory():
21 print("打印目錄")
22 class Son(Father):
23 def __init__(self):
24 super().__init__()
25 Father.__init__(self)
26 def operate(self):
27 print("我的操作猛如虎")
28 xiaoming=Son()
29 print(id(xiaoming))
以上就是類方法寫法和調(diào)用,類屬性寫法和調(diào)用,靜態(tài)方法寫法和調(diào)用,魔法方法有和調(diào)用具體使用,這里不過分描述。
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。