组合
1、定义一个人的类
class Person: def __init__(self,id_num,name,Hand,Foot,Trunk,Head): self.id_num=id_num self.name=name self.hand=Hand self.foot=Foot self.trunk=Trunk self.head=Head
2、可以定义一些属于人的这些部件的小类,在大类Person里面实例化这些小类
class Hand: pass class Foot: pass class Trunk: pass class Head: pass class Person: def __init__(self,id_num,name): self.id_num=id_num self.name=name self.hand=Hand() self.foot=Foot() self.trunk=Trunk() self.head=Head() p1=Person("123","beiye") print(p1.__dict__)
3、定义一个学校和课程的类
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,period,school): self.name=name self.price=price self.period=period self.school=school s1=School("清华","北京")#生产3个学校的实例 s2=School("清华","南京") s3=School("清华","东京") # c1=Course("linux","10","1h","清华") c1=Course("linux","10","1h",s1)#把s1传给course的school这个参数位置 print(c1.__dict__) C:\python35\python3.exe D:/pyproject/day25/组合.py {'school': <__main__.School object at 0x0000000000B54668>, 'name': 'linux', 'price': '10', 'period': '1h'}
4、现在c1的属性字典里面有school这个对象了,如何通过c1这个实例,查出来school的地址呢
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,period,school): self.name=name self.price=price self.period=period self.school=school s1=School("清华","北京") s2=School("清华","南京") s3=School("清华","东京") # c1=Course("linux","10","1h","清华") c1=Course("linux","10","1h",s1) print(c1.__dict__)#查看c1这个实例的属性字典,里面的school就是s1 print(c1.school)#c1.school等价于s1,那如何取学校的名字呢 print(s1) C:\python35\python3.exe D:/pyproject/day25/组合.py {'name': 'linux', 'price': '10', 'period': '1h', 'school': <__main__.School object at 0x0000000000D54668>} <__main__.School object at 0x0000000000D54668> <__main__.School object at 0x0000000000D54668> 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,period,school): self.name=name self.price=price self.period=period self.school=school s1=School("清华","北京") s2=School("清华","南京") s3=School("清华","东京") # c1=Course("linux","10","1h","清华") c1=Course("linux","10","1h",s1) print(c1.__dict__)#查看c1这个实例的属性字典,里面的school就是s1 print(c1.school.name)#这样就通过c1取出来了学校的名字,这就是一个组合 C:\python35\python3.exe D:/pyproject/day25/组合.py {'price': '10', 'name': 'linux', 'period': '1h', 'school': <__main__.School object at 0x0000000000D74668>} 清华
5、选课系统,如何让用户选择
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,period,school): self.name=name self.price=price self.period=period self.school=school s1=School("清华","北京") s2=School("清华","南京") s3=School("清华","东京") msg=''' 1 清华 北京校区 2 清华 南京校区 3 清华 东京校区 ''' while True: print(msg)#调用提示信息 menu={ "1":s1, "2":s2, "3":s3 } choice=input("选择学校>>>>>")#得到用户的输入之后,就调用字典 school_obj=menu[choice]#得到用户选择的校区 name=input("课程名>>>>")# price = input("价格>>>>") period = input("周期>>>>") new_course=Course(name,price,period,school_obj)#生成一个新的课程类 print("课程【%s】属于[%s]学校【%s】校区" %(new_course.name,new_course.school.name,new_course.school.addr)) #调用新课程和学校名,和校区名字 选择学校>>>>>2 课程名>>>>python 价格>>>>100 周期>>>>1month 课程【python】属于[清华]学校【南京】校区