日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python面向对象编程之组合

發布時間:2024/9/21 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python面向对象编程之组合 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前面講了面向類與對象的繼承,知道了繼承是一種什么“是”什么的關系。

然而類與類之間還有另一種關系,這就是組合

先來看兩個例子:
先定義兩個類,一個老師類,老師類有名字,年齡,出生的年,月和日,所教的課程等特征以及走路,教書的技能。

class Teacher:def __init__(self,name,age,year,mon,day):self.name=nameself.age=ageself.year=yearself.mon=monself.day=daydef walk(self):print("%s is walking slowly"%self.name)def teach(self):print("%s is teaching"%self.name)

再定義一個學生類,學生類有名字,年齡,出生的年,月和日,學習的組名等特征以及走路,學習的技能

class Student:def __init__(self,name,age,year,mon,day):self.name=nameself.age=ageself.year=yearself.mon=monself.day=daydef walk(self):print("%s is walking slowly"%self.name)def study(self):print("%s is studying"%self.name)

根據類的繼承這個特性,可以把代碼縮減一下。

定義一個人類,然后再讓老師類和學生類繼承人類的特征和技能:

class People:def __init__(self,name,age,year,mon,day):self.name=nameself.age=ageself.year=yearself.mon=monself.day=daydef walk(self):print("%s is walking"%self.name)class Teacher(People):def __init__(self,name,age,year,mon,day,course):People.__init__(self,name,age,year,mon,day)self.course=coursedef teach(self):print("%s is teaching"%self.name)class Student(People):def __init__(self,name,age,year,mon,day,group):People.__init__(self,name,age,year,mon,day)self.group=groupdef study(self):print("%s is studying"%self.name)

再對老師和學生進行實例化,得到一個老師和一個學生。

t1=Teacher("alex",28,1989,9,2,"python") s1=Student("jack",22,1995,2,8,"group2")

現在想知道t1和s1的名字,年齡,出生的年,月,日都很容易,但是想一次性打印出
t1或s1的生日就不那么容易了,這時就需要用字符串進行拼接了,有沒有什么更好的辦法呢??

那就是組合。

繼承是一個子類是一個父類的關系,而組合則是一個類有另一個類的關系。

可以說每個人都有生日,而不能說人是生日,這樣就要使用組合的功能 。
可以把出生的年月和日另外再定義一個日期的類,然后用老師或者是學生與這個日期的類
組合起來,就可以很容易得出老師t1或者學生s1的生日了,再也不用字符串拼接那么麻煩了。
來看下面的代碼:

class Date:def __init__(self,year,mon,day):self.year=yearself.mon=monself.day=daydef birth_info(self):print("The birth is %s-%s-%s"%(self.year,self.mon,self.day))class People:def __init__(self,name,age,year,mon,day):self.name=nameself.age=ageself.birth=Date(year,mon,day)def walk(self):print("%s is walking"%self.name)class Teacher(People):def __init__(self,name,age,year,mon,day,course):People.__init__(self,name,age,year,mon,day)self.course=coursedef teach(self):print("%s is teaching"%self.name)class Student(People):def __init__(self,name,age,year,mon,day,group):People.__init__(self,name,age,year,mon,day)self.group=groupdef study(self):print("%s is studying"%self.name) t1=Teacher("alex",28,1989,9,2,"python") s1=Student("jack",22,1995,2,8,"group2")

這樣一來,可以使用跟前面一樣的方法來調用老師t1或學生s1的姓名,年齡等特征
以及走路,教書或者學習的技能。

print(t1.name) t1.walk() t1.teach()

輸出為:

alex alex is walking alex is teaching

那要怎么能夠知道他們的生日呢:

print(t1.birth)

輸出為:

<__main__.Date object at 0x0000000002969550>

這個birth子類Teacher從父類People繼承過來的,而父類People的birth又是與Date這個類組合在一
起的,所以,這個birth是一個對象。而在Date類下面有一個birth_info的技能,這樣就可以通過
調用Date下面的birth_info這個函數屬性來知道老師t1的生日了。

t1.birth.birth_info()

得到的結果為:

The birth is 1989-9-2

同樣的,想知道實例學生s1的生日也用同樣的方法:

s1.birth.birth_info()

得到的結果為:

The birth is 1995-2-8

組合就是一個類中使用到另一個類,從而把幾個類拼到一起。組合的功能也是為了減少重復代碼。

總結

以上是生活随笔為你收集整理的python面向对象编程之组合的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。