类中的组合:大类中包含着小类,作用:类与类之间做关联
class Hand: passclass Foot: passclass Head: passclass Person: def __init__(self,name): self.name=name self.hand=Hand()#实例化 self.foot=Foot()#实例化 self.head=Head()#实例化p1=Person('sxj')print(p1.__dict__)
class School: def __init__(self,name,addr): self.name=name self.addr=addr def zhao_sheng(self): print('%s在招生'%self.name)class Course: def __init__(self,name,price,time,school): self.name=name self.price=price self.time=time self.school=schools1=School('oldboy','sh')s2=School('oldboy','bj')s3=School('oldboy','dj')c1=Course('linux',10,'1h',s1)#上海校区linux课程menu={ '1':s1, '2':s2, '3':s3,}msg='' \ '1 oldboy sh \n' \ '2 oldboy bj \n' \ '3 oldboy dj \n'while True: print(msg) #输入提示信息 choice=input('请输入:') school_obj=menu[choice] name=input('课程名字>>>>') price=input('money>>>>') time=input('课程时间>>>>') new_course=Course(name,price,time,school_obj) print(new_course.__dict__) print('课程名字[%s]属于[%s]学校'%(new_course.name,new_course.school.name))
》》》》》》
1 oldboy sh
2 oldboy bj 3 oldboy dj请输入:3
课程名字>>>>pythonmoney>>>>5yuan课程时间>>>>1h{'name': 'python', 'price': '5yuan', 'time': '1h', 'school': <__main__.School object at 0x102198f98>}课程名字[python]属于[oldboy]学校