Python学习第十七课——组合
Posted PY憨
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python学习第十七课——组合相关的知识,希望对你有一定的参考价值。
组合1
#组合 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(‘111‘,‘join‘)
print(p1.__dict__)
组合2
class School:
def __init__(self, name, addr):
self.name = name
self.addr = addr
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(‘市里蹲‘, ‘开封‘)
c1 = Course(‘python‘, 10, ‘10天‘, s1)
c2 = Course(‘python‘, 100, ‘100天‘, s2)
# print(c1.__dict__)
print(c1.school.name) # 家里蹲
print(c2.school.name) # 市里蹲
print(c1.school.addr) # 郑州
print(c2.school.addr) # 开封
组合例子之点课系统
# 点课系统
class School:
def __init__(self, name, addr):
self.name = name
self.addr = addr
class Teacher: # 老师应该与课程进行关联
def __init__(self, name, gongzi,course):
self.name = name
self.gongzi = gongzi
self.course=course
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
s1 = School(‘家里蹲‘, ‘郑州‘)
s2 = School(‘市里蹲‘, ‘开封‘)
s3 = School(‘县里蹲‘, ‘兰考‘)
t1 = Teacher(‘憨憨‘, 10000, ‘python‘)
t2 = Teacher(‘玫玫‘, 1000, ‘安卓‘)
t3 = Teacher(‘浩浩‘, 100, ‘c‘)
msg = ‘‘‘
1 家里蹲 郑州校区
2 市里蹲 开封校区
3 县里蹲 兰考校区
‘‘‘
msg1 = ‘‘‘
4 憨憨 python
5 玫玫 安卓
3 浩浩 c
‘‘‘
while True:
print(msg)
menu = {
‘1‘: s1,
‘2‘: s2,
‘3‘: s3
}
print(msg1)
teacher_menu = {
‘4‘: t1,
‘5‘: t2,
‘6‘: t3,
}
choice = input(‘选择学校>>>‘)
school_obj = menu[choice] # 获取学校信息
name = input(‘选择课程>>>‘)
teacher_obj = teacher_menu[name] # 获取老师信息
gongzi = t1.gongzi
price = input(‘选择费用>>>‘)
period = input(‘选择周期>>>‘)
teacher = input(‘选择老师>>>‘)
new_course = Course(name, price, period, school_obj, teacher_obj)
# print(new_teacher.__dict__)
print(‘课程【%s】属于【%s】学校 老师为 %s‘ % (new_course.teacher.course, new_course.school.name, new_course.teacher.name))
#课程【python】属于【家里蹲】学校 老师为 憨憨
以上是关于Python学习第十七课——组合的主要内容,如果未能解决你的问题,请参考以下文章