Learning-Python【24】:面向对象之组合

时间:2024-04-24 19:05:14

组合的概念

一个类的对象具备某一个属性,该属性的值是来自于另外一个类的对象,称之为组合,组合也是用来解决类与类之间代码冗余的问题

组合的使用

在继承中,举了一个属性查找顺序方式一的例子,最终的实现成果如下

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,但并不合理,并不是所有 “湫兮如风学院” 的人都有这些课程信息,如果还有个管理员,他并不需要这些课程信息。还有教师与学生都属于 “湫兮如风学院” 的人,这是一个从属关系,可以使用继承,但课程与老师、课程与学生之间没有从属关系,仅仅是老师和学生有课程这种属性。所以此时考虑再定义一个类,用来存放课程信息,并实例化出两门课程

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>

建立了关系,便可以查看学生与老师的课程信息,但上面的输出操作并不能查看到学生选了哪门课程,老师选了哪门课程,查看的并不是课程的详细信息,于是需要进一步访问学生课程信息的课程名、课程周期、课程价格,同样老师的课程信息也需要进一步才能访问

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

这样确实能够访问到课程的详细信息,但是你会发现,我做了同样的事情,每次都要先去访问课程才能访问到课程的详细信息,于是可以考虑将课程的详细信息作为一个功能(函数),每次访问通过学生的课程或老师的课程去访问,也就是说把学生的课程或老师的课程当作参数传入这个函数,然后再去访问课程的详细信息

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 自动传入,然后课程对象就可以调用这个功能去查看课程的相关信息

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

以上就是组合的应用,让一个类的对象具备一个属性,这个属性的值来自于另外一个类的对象,这就是把两个类组合到一起