1 class School: 2 def __init__(self, name, addr): 3 self.name = name 4 self.addr = addr 5 6 class Teacher: 7 def __init__(self,name,gender,age): 8 self.name = name 9 self.gender = gender 10 self.age = age 11 12 class Course: 13 def __init__(self, name, price, period, school): 14 self.name = name 15 self.price = price 16 self.period = period 17 self.school = school 18 19 20 s1 = School('oldboy', '北京') 21 s2 = School('oldboy', '南京') 22 s3 = School('oldboy', '东京') 23 # c1 = Course('linux',10,1,s1) 24 # print(c1.school.name) 25 msg = '''1 老男孩 北京校区 26 2 老男孩 南京校区 27 3 老男孩 东京校区 28 ''' 29 while True: 30 menu = { 31 '1': s1, 32 '2': s2, 33 '3': s3 34 } 35 print(msg) 36 choice = input('请输入选择的校区》》》') 37 school_obj = menu[choice] 38 course = input('请输入选择的课程》》》') 39 price = input('请输入缴纳的价格》》》') 40 period = input('请输入学习的周期》》》') 41 course_obj = Course(course,price,period,school_obj) 42 print(course_obj.school.name) 43 输出: 44 1 老男孩 北京校区 45 2 老男孩 南京校区 46 3 老男孩 东京校区 47 48 请输入选择的校区》》》2 49 请输入选择的课程》》》python 50 请输入缴纳的价格》》》10000 51 请输入学习的周期》》》3个月 52 oldboy