1. 使用类实现一个校园管理系统
class School(): def __init__(self,course): # 一个学校同时开两门课应该怎么办,这里只能传一个course对象作为属性 self.course=course # course是Course类的对象 class Course(): def __init__(self,name,period,price): self.name=name self.period=period self.price=price class Class(): # 前面老师已经关联班级的情况下,班级怎么关联老师啊, def __init__(self,name,course): self.name=name self.course=course class Student(): def __init__(self,name,password,school,classes): self.name=name self.password=password self.school=school self.classes=classes class Teacher(): def __init__(self,name,password,school,classes,course): self.name=name self.password=password self.school=school self.classes=classes self.course=course if __name__=='__main__': linux = Course('linux', '3 mon', 10000) python = Course('python', '2 mon', 20000) go = Course('go', '1 mon', '50000') beijing = School(python) shanghai = School(go) class_1 = Class('class_1', python) teacher_1 = Teacher('alex', '123', beijing, class_1, python) student_1 = Student('xuanxuan', '123', beijing, class_1) username=input(">>>") password=input(">>>") if username==student_1.name and password==student_1.password: print(student_1.school.course.name) print(student_1.classes.name) elif username==teacher_1.name and password==teacher_1.password: print(teacher_1.classes.name) print(teacher_1.course.name) # print("管理自己的班级功能有待完善") elif username=='Admin' and password=='Admin': class_2=Class('class_2', linux) teacher_2=Teacher('Eva-J', '123', beijing, class_2, linux) java = Course('Java', '4 mon', '10000') print('Admin创建的教师名字:', teacher_2.name) print('Admin创建的班级名字:',class_2.name) print('Admin创建的课程名字:', go.name) else: print('用户名或密码错误!')
运行结果:
学生视角登录:----查看课程,查看班级
教师视角登录: ----查看班级 课程:
管理员视角登录:----创建讲师,创建班级,创建课程
版本二(根据Eva-J的流程图来的)-----没有完成最后一步,把产生的数据使用Pickle写进文件,然后账号和密码我都是先调用Admin()来查找的
class School(): def __init__(self,name,place,course_dict): self.name=name self.place=place self.course_dict=course_dict class Teacher(): def __init__(self,name,school,course,clas,students_dict): self.name=name self.school=school self.course=course # 属性 course 是Course类的对象 self.clas=clas # clas 只是一个普通属性 self.students_dict=students_dict def view(self): print("1: 查看自己讲的课程信息") print("2: 查看自己讲的班级") print("3: 查看自己带的学生") info=input(">>>") if info=='1': print(self.course.name) elif info=='2': print(self.clas) elif info=='3': for i in self.students_dict: print(self.students_dict[i].name) else: print("输入有误!") class Course(): def __init__(self,name,period,price): self.name=name self.period=period self.price=price class Student(): def __init__(self,name,clas,school): self.name=name self.clas=clas self.school=school def view(self): print("1: 选择自己的班级") print("2: 查看自己的信息") info=input('>>>') if info=='1': print(self.clas) elif info=='2': print(self.name) class Admin(): def __init__(self,name): self.name=name python = Course('python', '2 mon', 10000) linux = Course('linux', '3 mon', 20000) go = Course('go', '4 mon', 15000) course_dict_1 = {1: python, 2: linux} course_dict_2 = {1: go} beijing = School('希望小学', '北京', course_dict_1) shanghai = School('幸福小学', '上海', course_dict_2) self.student_1 = Student('璇璇', '1班', beijing) self.student_2 = Student('七七', '1班', beijing) self.student_3 = Student('脸脸', '2班', shanghai) self.student_4 = Student('猪猪', '2班', shanghai) students_dict_1 = {1: self.student_1, 2: self.student_2} students_dict_2 = {1: self.student_3, 2: self.student_4} self.alex = Teacher('alex', beijing, python, '1班', students_dict_1) self.Eva = Teacher('Eva-J', shanghai, go, '2班', students_dict_2) if __name__=="__main__": username=input("username:") password=input("password:") if username=='admin' and password=='admin': admin = Admin("admin") print("欢迎您,%s" % admin.name) elif username=='璇璇' and password=='123': admin = Admin("admin") admin.student_1.view() elif username=='alex' and password=='123': admin = Admin("admin") admin.alex.view()
运行结果: