Python面向对象 组合(选课系统示例)
1 # Author : Kelvin 2 # Date : 2019/1/15 20:44 3 4 """ 5 学校与老师关联 6 课程与老师和学校关联 7 """ 8 9 10 class School(): 11 def __init__(self, name, profession): 12 self.name = name 13 self.profession=profession 14 15 class Teacher(): 16 def __init__(self, name, age, gender, school): 17 self.name = name 18 self.age = age 19 self.gender = gender 20 self.school=school 21 22 23 class Course(): 24 def __init__(self, name, time, school, teacher): 25 self.name = name 26 self.time = time 27 self.school = school 28 self.teacher = teacher 29 30 31 s1 = School("大庆师范学院", "计算机专业") 32 s2 = School("大庆师范学院", "生物工程专业") 33 s3 = School("大庆师范学院", "外国语专业") 34 35 t1 = Teacher("kelvin", 22, "male", s1) 36 t2 = Teacher("alex", 23, "male", s3) 37 t3 = Teacher("jam", 25, "female", s2) 38 39 40 msg1=""" 41 学校选项: 42 1:"大庆师范学院", "计算机专业" 43 2:"大庆师范学院", "生物工程专业" 44 3:"大庆师范学院", "外国语专业" 45 """ 46 msg2=""" 47 教师选项: 48 1:"kelvin", 22, "male", "大庆师范学院", "计算机专业" 49 2:"alex", 23, "male", "大庆师范学院", "外国语专业" 50 3:"jam", 25, "female","大庆师范学院", "生物工程专业" 51 """ 52 while True: 53 print(msg1) 54 choice1 = input("请选择学校专业:") 55 s = { 56 "1": s1, 57 "2": s2, 58 "3": s3 59 } 60 if choice1 in s: 61 sx = s[choice1] 62 else: 63 print("专业选择异常,请重试!") 64 break 65 66 print(msg2) 67 choice2 = input("请选择老师:") 68 t = { 69 "1": t1, 70 "2": t2, 71 "3": t3 72 } 73 if choice2 in t: 74 tx = t[choice2] 75 else: 76 print("教师选择异常,请重试!") 77 break 78 course_name = input("请输入课程名:") 79 course_time = input("请输入课程时长:") 80 new_course = Course(course_name, course_time, sx, tx) 81 print("选课成功! [%s]课程所属的学校是[%s]专业是[%s] 老师名字[%s]年龄[%s]性别[%s]所属学校[%s]专业[%s] 课程时长:[%s]" % ( 82 new_course.name, new_course.school.name, new_course.school.profession, new_course.teacher.name, 83 new_course.teacher.age, new_course.teacher.gender, new_course.teacher.school.name,new_course.teacher.school.profession,new_course.time)) 84 break