标签:price linu run input linux ldb 面向对象 def while
1、组合模式
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(‘111111‘,‘alex‘)
2、组合练习
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(‘oldboy‘,‘北京‘) s2=School(‘oldboy‘,‘南京‘) s3=School(‘oldboy‘,‘东京‘) c1=Course(‘linux‘,10,‘1h‘,s1) print(c1.__dict__) #{‘name‘: ‘linux‘, ‘price‘: 10, ‘period‘: ‘1h‘, ‘school‘: <__main__.School object at 0x000001BD46E57BE0>} print(c1.school.name)#oldboy
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,teacher): self.name=name self.price=price self.period=period #组合的方式建关联 self.school=school self.teacher=teacher class Teacher: def __init__(self,name,age,sex,school): self.name=name self.age=age self.sex=sex self.school=school def tea_stu(self): print(‘%s 老师正在上课‘%self.name) s1=School(‘oldboy‘,‘北京‘) s2=School(‘oldboy‘,‘南京‘) s3=School(‘oldboy‘,‘东京‘) # c1=Course(‘linux‘,10,‘1h‘,s1) msg=‘‘‘ 1 老男孩 北京校区 2 老男孩 南京校区 3 老男孩 东京校区 ‘‘‘ while True: print(msg) menu={ ‘1‘:s1, ‘2‘:s2, ‘3‘:s3, } choice=input(‘选择学校>>: ‘).strip() school_obj=menu[choice] t1=Teacher(‘alex‘,18,‘male‘,school_obj) t2=Teacher(‘egon‘,22,‘male‘,school_obj) t3=Teacher(‘shanshan‘,35,‘female‘,school_obj) msg1=‘‘‘ 1 alex老师 2 geon老师 3 shanshan老师 ‘‘‘ print(msg1) menu1={ ‘1‘:t1, ‘2‘:t2, ‘3‘:t3, } tea_name=input(‘选择代课老师的姓名>>: ‘).strip() teacher_obj=menu1[tea_name] name=input(‘课程名>>: ‘).strip() price=input(‘课程费用>>: ‘).strip() period=input(‘课程周期>>: ‘).strip() new_course=Course(name,price,period,school_obj,teacher_obj) print(‘‘‘ ------------选课详细信息------------ 学校:%s 地址:%s 老师:%s 年龄:%s 性别%s 课程:%s 费用:%s 周期:%s ‘‘‘%(new_course.teacher.school.name,new_course.teacher.school.addr,new_course.teacher.name,new_course.teacher.age,new_course.teacher.sex,new_course.name,new_course.price,new_course.period))
2018-09-03 22:05:57
标签:price linu run input linux ldb 面向对象 def while
原文地址:https://www.cnblogs.com/happyfei/p/9581451.html