课后练习 day24 面向对象三大特性之继承的练习
day24作业
第一题:选课系统(简单版)
作业要求如下:
#类定义出来:学校、学员、课程、讲师
#属性和方法先自己分析
#用上继承
程序代码:
# # # 第一题:选课系统(初始化)
'''
要求如下:
#类定义出来:学校、学员、课程、讲师
#属性和方法先自己分析
#用上继承
'''
# 定义学校类
class School:
school_area_name = None
school = 'oldboy'
def __init__(self,school_area_name,school_address,course):
self.school_area_name = school_area_name # 学校校区名
self.school_address = school_address # 学校校区地址
self.course = course # 该校区开设的课程
# 定义学生和老师的基类
class Personnel:
school = 'oldboy' # 所属学校
def __init__(self,name,age,school_area_name):
self.school_area_name = school_area_name # 所属校区
self.name = name # 基类的名字
self.age = age # 基类的年龄
# 定义讲师类
class Teacher(Personnel):
school = Personnel.school
def __init__(self,name,age,class_no,school_area_name):
Personnel.__init__(self,name,age,school_area_name) # 继承基类
self.class_no = class_no # 班级编号
def update_grade(self,target): # 修改学生成绩
Student.grade = target
# 定义学生类
class Student(Personnel):
school = Personnel.school
def __init__(self,name,age,class_no,course,school_area_name,grade=0):
Personnel.__init__(self,name,age,school_area_name) # 继承基类
self.class_no = class_no # 班级编号
self.grade = grade # 学生课程成绩
self.course = course
def choice_course(self,course): # 定义一个选课方法
Student.course = course # 学生选择的课程名
return Student.course
# 定义课程类
class Course:
def __init__(self,course,money,time):
self.course = course # 课程名称
self.money = money # 课程费用
self.time = time # 课程时间
# 定义学校实例
school1 = School('上海校区','上海虹桥',['linux','数据库'])
school2 = School('深圳校区','北京青浦',['web前端','java','python'])
# 定义课程实例
course1 = Course('linux',18000,'6m')
course2 = Course('python',20000,'5m')
# 定义教师实例
teacher1 = Teacher('nick','18','py10',school1.school_area_name)
teacher2 = Teacher('tank','24','py11',school2.school_area_name)
# 定义学生实例
student1 = Student('forever','16','py11',course1.course,school1.school_area_name)
student2 = Student('abc','20','py11',course1.course,school1.school_area_name)
print(f'学生:{student1.name}的所选课程是:{student1.choice_course(course1.course)} 他的上课校区在:{student1.school_area_name} 他的任课老师是:{teacher1.name if student1.class_no == teacher1.class_no else teacher2.name} ')
上午讲课内容整理
https://www.cnblogs.com/foreversun92/p/11419360.html