选课系统项目中涉及到诸多数据与功能,要求引入面向对象的思想对其进行高度整合
1、学校数据与功能整合
2、课程数据与功能进行整合
3、学生数据与功能进行整合
4、讲师数据与功能进行整合
5、班级数据与功能进行整合
ps:不会写的同学,可以先用普通的方式,先把数据与功能都给写好,再考虑基于面向对象的思想进行整合
数据部分:
校区的名字:如"老男孩上海校区"
校区的地址:如"上海虹桥"
班级名字
班级所在校区
学生的学校
学生的姓名
学生的年龄
学号
学生的性别
课程名字
课程周期
课程价格
老师的名字
老师的年龄
老师的薪资
老师的等级
功能部分:
校区创建完毕后,可以为每个校区创建班级
班级创建完毕后,可以为每个班级创建课程
学生创建完毕后,学生可以选择班级
老师创建完毕后,可以为学生打分
class School: def __init__(self, school_name, school_address): self.school_name = school_name self.school_address = school_address def school_info(self): print("校区地址:{} 校区名字:{}".format(self.school_address, self.school_name)) def classroom_join(self, classroom_address, classroom_name): res = Classroom(classroom_address, classroom_name) return res class Classroom: classroom = [] def __init__(self, classroom_address, classroom_name): self.classroom_name = classroom_name self.classroom_address = classroom_address def class_info(self): print("班级名字:{} 班级所在的校区名字:{}".format(self.classroom_name, self.classroom_address)) def add_course(self, course_name, course_date, course_price): res = Course(course_name, course_date, course_price) return res class Course: def __init__(self, course_name, course_time, course_price): self.course_name = course_name self.course_time = course_time self.course_price = course_price def course_info(self): print("课程名字:{} 课程周期:{}月 课程价格:{}".format(self.course_name, self.course_time, self.course_price)) class Student: def __init__(self, student_school, student_name, student_age, student_number, student_gender): self.student_school = student_school self.student_name = student_name self.student_age = student_age self.student_number = student_number self.student_gender = student_gender def student_info(self): print("学生学校:{} 学生姓名:{} 学生年龄:{} 学生学号:{} 学生性别:{} ". format(self.student_school, self.student_name, self.student_age, self.student_number, self.student_gender)) def class_choice(self, student, classroom): print(f'{student}选择了{classroom}') class Teacher: def __init__(self, teacher_name, teacher_age, teacher_salary, teacher_grade): self.teacher_name = teacher_name self.teacher_age = teacher_age self.teacher_salary = teacher_salary self.teacher_grade = teacher_grade def teacher_info(self): print("老师的名字:{} 老师的年龄:{} 老师的薪资:{} 老师的等级:{}". format(self.teacher_name, self.teacher_age, self.teacher_salary, self.teacher_grade)) def get_score(self, student_name, score): print(f'{student_name}得到了{score}分') # 主程序 a = School('oldboy_shanghai', '上海') a.school_info() b = a.classroom_join('oldboy_shanghai', 'py14') b.class_info() c = b.add_course('python', 6, 21800) c.course_info() d = Student('oldboy_shanghai', 'lucky', 18, '007', '女') d.student_info() d.class_choice('lucky', 'py14') f = Teacher('egon', 18, 50000, '天下第一teacher') f.teacher_info() f.get_score('lucky', 50)
# 整合->解耦合->扩展性增强 class School: school_name = 'OLDBOY' def __init__(self, nickname, addr): self.nickname = nickname self.addr = addr self.classes = [] def related_class(self, class_obj): # self.classes.append(班级名字) # self.classes.append(class_name) self.classes.append(class_obj) def tell_class(self): # 改 # 打印的班级的名字 print(self.nickname.center(60,'=')) # 打印班级开设的课程信息 for class_obj in self.classes: class_obj.tell_course() # # 一:学校 # #1、创建校区 school_obj1=School('老男孩魔都校区','上海') school_obj2=School('老男孩帝都校区','北京') # # #2、为学校开设班级 # # 上海校区开了:脱产14期,上海校区开了脱产15期 # school_obj1.related_class("脱产14期") # school_obj1.related_class("脱产15期") # # # 北京校区开了:脱产29期 # school_obj2.related_class("脱产29期") # # #3、查看每个校区开设的班级 # school_obj1.tell_class() # school_obj2.tell_class() class Class: def __init__(self, name): self.name = name self.course = None def related_course(self, course_obj): # self.course = course_name self.course = course_obj def tell_course(self): print('%s' % self.name,end=" ") self.course.tell_info() # 打印课程的详细信息 # 二:班级 # 1、创建班级 class_obj1 = Class('脱产14期') class_obj2 = Class('脱产15期') class_obj3 = Class('脱产29期') # 2、为班级关联一个课程 # class_obj1.related_course('python全栈开发') # class_obj2.related_course('linux运维') # class_obj3.related_course('python全栈开发') # 3、查看班级开设的课程信息 # class_obj1.tell_course() # class_obj2.tell_course() # class_obj3.tell_course() # 4、为学校开设班级 # 上海校区开了:脱产14期,上海校区开了脱产15期 school_obj1.related_class(class_obj1) school_obj1.related_class(class_obj2) # 北京校区开了:脱产29期 school_obj2.related_class(class_obj3) # school_obj1.tell_class() # school_obj2.tell_class() class Course: def __init__(self,name,period,price): self.name=name self.period=period self.price=price def tell_info(self): print('<课程名:%s 周期:%s 价钱:%s>' %(self.name,self.period,self.price)) # 三:课程 # 1、创建课程 course_obj1=Course('python全栈开发','6mons',20000) course_obj2=Course('linux运维','5mons',18000) # 2、查看课程的详细信息 # course_obj1.tell_info() # course_obj2.tell_info() # 3、为班级关联课程对象 class_obj1.related_course(course_obj1) class_obj2.related_course(course_obj2) class_obj3.related_course(course_obj1) # class_obj1.tell_course() # class_obj2.tell_course() # class_obj3.tell_course() school_obj1.tell_class() school_obj2.tell_class() class Student: pass