Learning-Python【24】:面向对象之组合
组合的概念
一个类的对象具备某一个属性,该属性的值是来自于另外一个类的对象,称之为组合,组合也是用来解决类与类之间代码冗余的问题
组合的使用
在继承中,举了一个属性查找顺序方式一的例子,最终的实现成果如下
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
以上就是组合的应用,让一个类的对象具备一个属性,这个属性的值来自于另外一个类的对象,这就是把两个类组合到一起
Learning-Python【24】:面向对象之组合的更多相关文章
- Python之面向对象的组合、多态、菱形问题、子类中重用父类的两种方式
一.组合 ''' 1.什么是组合 组合就是一个类的对象具备某一个属性,该属性的值是指向另外一个类的对象 2.为何用组合 组合也是用来解决类与类直接代码冗余问题的 3.如何用组合 ''' # 继承减少代 ...
- Python()- 面向对象的组合用法
面向对象的组合用法 一个类中以另一个类的对象作为数据属性(一个类中引用另一个类的对象)一种 "有" 的关系: 比如:定义 1个人类 & 1个武器类 然后 张三 有 枪 李四 ...
- python基础--面向对象基础(类与对象、对象之间的交互和组合、面向对象的命名空间、面向对象的三大特性等)
python基础--面向对象 (1)面向过程VS面向对象 面向过程的程序设计的核心是过程(流水线式思维),过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西. ...
- 《Python》 面向对象初识(组合)
一.面向对象(组合): 定义:给一个类的对象封装一个属性,这个属性是另一个类的对象. 意义:让类的对象与另一个类的对象产生关系,类与类之间产生关系. 人物使用武器攻击另一个人物: class Game ...
- Python OOP面向对象
一.什么是面向对象的程序设计 1.面向过程 程序设计:核心是过程二字,过程指的是解决问题的步骤,即先干什么再干什么......面向过程的设计就好比精心设计好一条流水线,是一种机械式的思维方式. 优点是 ...
- python学习------面向对象的程序设计
一 面向对象的程序设计的由来 1940年以前:面向机器 最早的程序设计都是采用机器语言来编写的,直接使用二进制码来表示机器能够识别和执行的指令和数 据.简单来说,就是直接编写 和 的序列来代表程序语言 ...
- Python进阶---面向对象的程序设计思想
Python的面向对象 一.面向过程与面向对象的对比 面向过程的程序设计的核心是过程(流水线式思维),过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西. 优 ...
- Python之面向对象一
引子 小游戏:人狗大战 角色:人和狗 角色属性:姓名,血量,战斗力和性别(种类) 技能:打/咬 用函数实现人打狗和狗咬人的情形 def Dog(name,blood,aggr,kind): dog = ...
- 【转】Python之面向对象与类
[转]Python之面向对象与类 本节内容 面向对象的概念 类的封装 类的继承 类的多态 静态方法.类方法 和 属性方法 类的特殊成员方法 继承层级关系中子类的实例对象对属性的查找顺序问题 一.面向对 ...
- python 历险记(二)— python 的面向对象
目录 前言 类和对象 如何定义和实例化类? 如何定义和使用属性? 什么是方法? 静态方法和普通方法 构造函数该怎么写? str 方法怎么写? 多态是什么? 继承性和 java 是一样的吗? 父类和子类 ...
随机推荐
- Promise及Async/Await
一.为什么有Async/Await? 我们都知道已经有了Promise的解决方案了,为什么还要ES7提出新的Async/Await标准呢? 答案其实也显而易见:Promise虽然跳出了异步嵌套的怪 ...
- java开发mis系统所需技术及其作用
MIS(管理信息系统--Management Information System)系统 ,是一个由人.计算机及其他外围设备等组成的能进行信息的收集.传递.存贮.加工.维护和使用的系统. 是一门新兴的 ...
- win10系统激活
我们常常使用一些激活工具来激活,效果可能比较差.比如我激活win10教育版,下载了很多软件无论如何都不能激活.但是使用命令行很容易就激活了. 1. 2.在命令提示符中依次输入: slmgr.vbs / ...
- Linux下/usr/bin与/usr/local/bin/区别总结
Linux下/usr/bin与/usr/local/bin/区别总结 2017年10月13日 12:30:17 2puT 阅读数:15930 版权声明:本文为博主原创文章! github地址:h ...
- 洛谷P1021邮票面值设计 [noip1999] dp+搜索
正解:dfs+dp 解题报告: 传送门! 第一眼以为小凯的疑惑 ummm说实话没看标签我还真没想到正解:D 本来以为这么多年前的noip应该不会很难:D 看来还是太菜了鸭QAQ 然后听说题解都可以被6 ...
- 更新Xcode10与iOS12 遇到的bug:library not found for -lstdc++.6.0.9
更新Xcode10与iOS12 遇到的bug:library not found for -lstdc++.6.0.9 解决办法:删除pod里导入的库文件,跑一下pod,再重新导入这些库文件,跑pod ...
- Python3学习之路~5.11 configparser模块
用于生成和修改常见配置文档,当前模块的名称在 python 2.x 版本中为 ConfigParser, python 3.x 版本中变更为 configparser. 来看一个好多软件的常见文档格式 ...
- [LeetCode] 236. Lowest Common Ancestor of a Binary Tree_ Medium tag: DFS, Divide and conquer
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- svn更新项目之后,项目报错一大堆并且tomcat部署项目时找不到项目
原因是:svn更新项目以后jdk路劲不对,需要使用自己安装的jdk,即可.具体步骤如下 第一步:右击项目-->Build path-->Configure Build path... 第二 ...
- AES,BigInteger,MD5加密
http://tool.oschina.net/apidocs/apidoc?api=jdk-zh package cn.com.gome.cashier.web; import java.lang. ...