組合的概念python
一個類的對象具有某一個屬性,該屬性的值是來自於另一個類的對象,稱之爲組合,組合也是用來解決類與類之間代碼冗餘的問題linux
組合的使用運維
在繼承中,舉了一個屬性查找順序方式一的例子,最終的實現成果以下ide
class People(): school = '湫兮如風學院'
def __init__(self, name, age, gender): self.name = name self.age = age self.gender = gender class Student(People): def __init__(self, name, age, gender, stu_id): People.__init__(self, name, age, gender) self.stu_id = stu_id def choose_course(self): print('%s正在選課' %self.name) class Teacher(People): def __init__(self, name, age, gender, level, salary): People.__init__(self, name, age, gender) self.level = level self.salary = salary def score(self, stu, num): print('教師%s給學生%s打%s分' %(self.name, stu.name, num)) stu.num = num stu = Student('qiu', 22, 'male') tea = Teacher('xi', 20, 'male', 10, 3000)
如今學生有課程信息,課程價格,課程週期,教師也有相同的信息,因而添加相應的數據屬性,注意這裏不能將這些信息放到對應的類裏面,假如我將一門課程放入學生類,那當我實例化一個學生對象時,他只能學習這門課程,若是我有多個課程呢?因此這些信息是對象獨有的,並非公有的函數
class People(): school = '湫兮如風學院'
def __init__(self, name, age, gender): self.name = name self.age = age self.gender = gender class Student(People): def __init__(self, name, age, gender, stu_id, course_name, course_period, course_price): People.__init__(self, name, age, gender) self.stu_id = stu_id self.course_name = course_name self.course_period = course_period self.course_price = course_price def choose_course(self): print('%s正在選課' %self.name) class Teacher(People): def __init__(self, name, age, gender, level, course_name, course_period, course_price): People.__init__(self, name, age, gender) self.level = level self.course_name = course_name self.course_period = course_period self.course_price = course_price def score(self, stu, num): stu.score = num print('教師%s給學生%s打%s分' % (self.name, stu.name, num))
但這樣寫出來你會發現,代碼又有重複部分了,能夠考慮將課程信息放到父類 People,但並不合理,並非全部 「湫兮如風學院」 的人都有這些課程信息,若是還有個管理員,他並不須要這些課程信息。還有教師與學生都屬於 「湫兮如風學院」 的人,這是一個從屬關係,可使用繼承,但課程與老師、課程與學生之間沒有從屬關係,僅僅是老師和學生有課程這種屬性。因此此時考慮再定義一個類,用來存放課程信息,並實例化出兩門課程linux運維
class Course(): def __init__(self, course_name, course_price, course_period): self.course_name = course_name self.course_price = course_price self.course_period = course_period class People(): school = '湫兮如風學院'
def __init__(self, name, age, gender): self.name = name self.age = age self.gender = gender class Student(People): def __init__(self, name, age, gender, stu_id): People.__init__(self, name, age, gender) self.stu_id = stu_id def choose_course(self): print('%s正在選課' %self.name) class Teacher(People): def __init__(self, name, age, gender, level): People.__init__(self, name, age, gender) self.level = level def score(self, stu, num): stu.score = num print('教師%s給學生%s打%s分' % (self.name, stu.name, num)) python = Course('Python全棧開發', '5 months', 3000) linux = Course('linux運維', '5 months', 2000)
這時課程與學生、課程與老師之間尚未創建關係,因此要作創建關係的操做學習
class Course(): def __init__(self, course_name, course_price, course_period): self.course_name = course_name self.course_price = course_price self.course_period = course_period class People(): school = '湫兮如風學院'
def __init__(self, name, age, gender): self.name = name self.age = age self.gender = gender class Student(People): def __init__(self, name, age, gender, stu_id): People.__init__(self, name, age, gender) self.stu_id = stu_id def choose_course(self): print('%s正在選課' %self.name) class Teacher(People): def __init__(self, name, age, gender, level): People.__init__(self, name, age, gender) self.level = level def score(self, stu, num): stu.score = num print('教師%s給學生%s打%s分' % (self.name, stu.name, num)) # 建立課程
python = Course('Python全棧開發', '5 months', 3000) linux = Course('linux運維', '5 months', 2000) # 建立學生和老師
stu1 = Student('夕顏', 19, 'male', 1001) tea1 = Teacher('東風', 19, 'female', 10) # 將學生、老師與課程對象關聯
stu1.course = python tea1.course = linux # 查看學生和老師的課程,
print(stu1.course) print(tea1.course) # 運行
<__main__.Course object at 0x000002081E998D68>
<__main__.Course object at 0x000002081E998DA0>
創建了關係,即可以查看學生與老師的課程信息,但上面的輸出操做並不能查看到學生選了哪門課程,老師選了哪門課程,查看的並非課程的詳細信息,因而須要進一步訪問學生課程信息的課程名、課程週期、課程價格,一樣老師的課程信息也須要進一步才能訪問spa
class Course(): def __init__(self, course_name, course_period, course_price, ): self.course_name = course_name self.course_price = course_price self.course_period = course_period class People(): school = '湫兮如風學院'
def __init__(self, name, age, gender): self.name = name self.age = age self.gender = gender class Student(People): def __init__(self, name, age, gender, stu_id): People.__init__(self, name, age, gender) self.stu_id = stu_id def choose_course(self): print('%s正在選課' %self.name) class Teacher(People): def __init__(self, name, age, gender, level): People.__init__(self, name, age, gender) self.level = level def score(self, stu, num): stu.score = num print('教師%s給學生%s打%s分' % (self.name, stu.name, num)) # 建立課程
python = Course('Python全棧開發', '5個月', 3000) linux = Course('linux運維', '5個月', 2000) # 建立學生和老師
stu1 = Student('夕顏', 19, 'male', 1001) tea1 = Teacher('東風', 19, 'female', 10) # 將學生、老師與課程對象關聯
stu1.course = python tea1.course = linux # 查看學生和老師的課程
print(stu1.course.course_name, stu1.course.course_period, stu1.course.course_price) print(tea1.course.course_name, tea1.course.course_period, tea1.course.course_price) # 運行
Python全棧開發 5個月 3000 linux運維 5個月 2000
這樣確實可以訪問到課程的詳細信息,可是你會發現,我作了一樣的事情,每次都要先去訪問課程才能訪問到課程的詳細信息,因而能夠考慮將課程的詳細信息做爲一個功能(函數),每次訪問經過學生的課程或老師的課程去訪問,也就是說把學生的課程或老師的課程看成參數傳入這個函數,而後再去訪問課程的詳細信息3d
class Course(): def __init__(self, course_name, course_period, course_price, ): self.course_name = course_name self.course_price = course_price self.course_period = course_period class People(): school = '湫兮如風學院'
def __init__(self, name, age, gender): self.name = name self.age = age self.gender = gender class Student(People): def __init__(self, name, age, gender, stu_id): People.__init__(self, name, age, gender) self.stu_id = stu_id def choose_course(self): print('%s正在選課' %self.name) class Teacher(People): def __init__(self, name, age, gender, level): People.__init__(self, name, age, gender) self.level = level def score(self, stu, num): stu.score = num print('教師%s給學生%s打%s分' % (self.name, stu.name, num)) # 建立課程
python = Course('Python全棧開發', '5個月', 3000) linux = Course('linux運維', '5個月', 2000) # 建立學生和老師
stu1 = Student('夕顏', 19, 'male', 1001) tea1 = Teacher('東風', 19, 'female', 10) # 將學生、老師與課程對象關聯
stu1.course = python tea1.course = linux def tell_info(course_obj): print(course_obj.course_name, course_obj.course_period, course_obj.course_price) # 查看學生和老師課程的詳細信息
tell_info(stu1.course) tell_info(tea1.course)
這時便不須要作一樣的事情了,每次只須要傳入一個課程對象,可是在調用的時候都是手動去傳入課程對象,我如今想實現的是讓它自動傳入一個課程對象,因而將它放入課程類中,經過 self 自動傳入,而後課程對象就能夠調用這個功能去查看課程的相關信息code
class Course(): def __init__(self, course_name, course_period, course_price, ): self.course_name = course_name self.course_price = course_price self.course_period = course_period def tell_info(self): print("課程名: %s | 課程週期: %s | 課程價格: %s"
%(self.course_name, self.course_period, self.course_price)) class People(): school = '湫兮如風學院'
def __init__(self, name, age, gender): self.name = name self.age = age self.gender = gender class Student(People): def __init__(self, name, age, gender, stu_id): People.__init__(self, name, age, gender) self.stu_id = stu_id def choose_course(self): print('%s正在選課' %self.name) class Teacher(People): def __init__(self, name, age, gender, level): People.__init__(self, name, age, gender) self.level = level def score(self, stu, num): stu.score = num print('教師%s給學生%s打%s分' % (self.name, stu.name, num)) # 建立課程
python = Course('Python全棧開發', '5個月', 3000) linux = Course('linux運維', '5個月', 2000) # 經過課程對象去查看課程的詳細信息
python.tell_info() linux.tell_info() # 建立學生和老師
stu1 = Student('夕顏', 19, 'male', 1001) tea1 = Teacher('東風', 19, 'female', 10) # 將學生、老師與課程對象關聯
stu1.course = python tea1.course = linux
學生和老師也能夠經過所選的課程去訪問該課程的詳細信息
class Course(): def __init__(self, course_name, course_period, course_price, ): self.course_name = course_name self.course_price = course_price self.course_period = course_period def tell_info(self): print("課程名: %s | 課程週期: %s | 課程價格: %s"
%(self.course_name, self.course_period, self.course_price)) class People(): school = '湫兮如風學院'
def __init__(self, name, age, gender): self.name = name self.age = age self.gender = gender class Student(People): def __init__(self, name, age, gender, stu_id): People.__init__(self, name, age, gender) self.stu_id = stu_id def choose_course(self): print('%s正在選課' %self.name) class Teacher(People): def __init__(self, name, age, gender, level): People.__init__(self, name, age, gender) self.level = level def score(self, stu, num): stu.score = num print('教師%s給學生%s打%s分' % (self.name, stu.name, num)) # 建立課程
python = Course('Python全棧開發', '5個月', 3000) linux = Course('linux運維', '5個月', 2000) # 經過課程對象去查看課程的詳細信息 # python.tell_info() # linux.tell_info()
# 建立學生和老師
stu1 = Student('夕顏', 19, 'male', 1001) tea1 = Teacher('東風', 19, 'female', 10) # 將學生、老師與課程對象關聯
stu1.course = python tea1.course = linux # 學生和老師所選課程的詳細信息
stu1.course.tell_info() tea1.course.tell_info() # 運行
課程名: Python全棧開發 | 課程週期: 5個月 | 課程價格: 3000 課程名: linux運維 | 課程週期: 5個月 | 課程價格: 2000
以上就是組合的應用,讓一個類的對象具有一個屬性,這個屬性的值來自於另一個類的對象,這就是把兩個類組合到一塊兒