{
"cells": [
{
"cell_type": "markdown",
"id": "51cac636",
"metadata": {},
"source": [
"# 五,类的继承,重写与多态\n",
" 1,继承,子类继承父类的方法\n",
" 2,重写,子类可以重写子类的方法(方法名称相同)\n",
" 3,多态,子类中方法名称相同但功能不同\n",
" 4,实例如下:"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "4118abfa",
"metadata": {},
"outputs": [],
"source": [
"# 定义一个父类person\n",
"class Person:\n",
" def __init__(self,name,sex,age):\n",
" self.name = name\n",
" self.sex = sex\n",
" self.age = age\n",
" \n",
" def working(self):\n",
" print('正在工作!')\n",
" \n",
" def show(self):\n",
" print('姓名:{}\\n性别:{}\\n年龄:{}\\n'.format(self.name,self.sex,self.age))\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "36d317b3",
"metadata": {},
"outputs": [],
"source": [
"#定义一个学生类Student\n",
"class Student(Person):\n",
" def __init__(self,name,sex,age,major):\n",
" super().__init__(name,sex,age) #用内置函数super()继承父类,调用父类初始化方法,相当于self.name = name #self.sex = sex #self.age = age \n",
" self.major = major\n",
" def studying(self):\n",
" print('正在学习!') \n",
" def show(self): #类的重写\n",
" #print('姓名:{}\\n性别:{}\\n年龄:{}\\n专业:{}\\n'.format(self.name,self.sex,self.age,self.major))\n",
" Person.show(self) #继承父类\n",
" print('专业:%s\\n'%self.major)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "c12c2c76",
"metadata": {},
"outputs": [],
"source": [
"#创建对象:\n",
"s = Student('严仕扬','男',20,'楼宇')"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "d4c73bf0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"姓名:严仕扬\n",
"性别:男\n",
"年龄:20\n",
"\n",
"专业:楼宇\n",
"\n"
]
}
],
"source": [
"s.show()"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "4bccc93e",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"正在学习!\n"
]
}
],
"source": [
"s.studying()"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "16701460",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"严仕扬 正在学习\n"
]
}
],
"source": [
"print(s.name,'正在学习')"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "366ff124",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"姓名:严仕扬\n",
"性别:男\n",
"年龄:20\n",
"\n",
"专业:楼宇\n",
"\n"
]
}
],
"source": [
"s.show()"
]
},
{
"cell_type": "markdown",
"id": "2990b0c1",
"metadata": {},
"source": [
"# 六,综合案例分析:\n",
" 1,定义一个父类student和两个子类levelst和passSt,在父类中定义2个实例变量,使子类继承父类的所有属性和方法,并在各自定义根据成绩\n",
" 计算等级和判断是否通过的方法;同时。将代码存储在student。py文件中。\n",
" 2,显示由levelt和passSt类对象构成的学生成绩列表,以及所有学生名字和成绩,并按字典排列\n"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "df2f30b0",
"metadata": {},
"outputs": [],
"source": [
"#定义student父类\n",
"class Student:\n",
" def __init__(self,name='',grade=0):\n",
" self._name=name\n",
" self._grade=grade\n",
" def show(self):\n",
" print('%s\\t %s'%(self.name,self.grade))"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "eca4b2f1",
"metadata": {},
"outputs": [],
"source": [
"class LeveISt(Student):\n",
" def calcLevel(self): #多态:方法名称相同功能不同·\n",
" if self.grade>=90:\n",
" return'A'\n",
" elif self.grade>=80:\n",
" return'B'\n",
" elif self.grade>=70:\n",
" return'C'\n",
" elif self.grade>=60:\n",
" return'D'\n",
" else:\n",
" return'E'\n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "00e558f4",
"metadata": {},
"outputs": [],
"source": [
"#(3)定义PassSt子类:\n",
"class PassSt(Student):\n",
" def calcLevel(self): #多态:方法名称相同功能不同\n",
" if self.grade>=60:\n",
" return'通过'\n",
" else:\n",
" return'不通过'\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5b5ea4f9",
"metadata": {},
"outputs": [],
"source": [
"#(4)定义列表函数list_ST存放学生数据\n",
"def list_St():\n",
" list_student=[]\n",
" is_continue='Y'\n",
" while is_continue in ['Y','y']:\n",
" name = input('请输入学生姓名:')\n",
" grade = float(input('请输入学生成绩:'))\n",
" choice = input('请选择成绩输出格式(LevelSt/PassSt)L/p:')\n",
" if choice.upper()=='L': #将输入的小写字母(l或p)转换为大写\n",
" s=LevelSt(name,grade) #创建对象s,并给属性赋值\n",
" else:\n",
" s=PassSt(name,grade)\n",
"\n",
" list_student.append(s) #将输入的学生信息追加到列表list_student里面\n",
" \n",
" is_continue = input('需要继续吗?(Y/N)') #需要输入多个学生的信息选择Y(y)\n",
" return list_student\n",
"\n",
"\n",
"#)5(定义显示处理后的学生信息)\n",
"def display(list_student):\n",
" print('\\n姓名\\t成绩\\n')\n",
" list_student.sort(key=lambda x:x.name,reverse = Ture) #将列表元素按字典由大到小排列\n",
" for each in list_student:\n",
" each.show() #将学生信息按行打印出来\n",
" \n",
"\n",
"#(6)定义主函数:\n",
"def main():\n",
" list_student = list_St() #调用list_St函数,获得数据\n",
" display(list_student) #将获得的数据作为实际参数传递给函数display形参\n",
"\n",
"#调用主函数:\n",
"if __name__== '__main__':\n",
" main()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "028f2220",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}