类的组合

class School:
    def __init__(self,name,addr):
        self.name = name
        self.addr = addr


class Courser:
    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,school,courser):
        self.name = name
        self.school = school
        self.course = courser



s1 = School("beida","北京")
s2 = School("zheda","杭州")

t1 = teacher("alex",s1,"python")
t2 = teacher("tea",s2,"linux")

# c1 = Courser("python","200","2",s1,t1)    #组合,一个类实例化传的参数是调用另一个类的实例化
#
# print(c1.school.name)

msg1 = """
1;北京
2:杭州
"""
msg2 = """
1:alex
2:tea
"""
while True:
    menu1 = {
        "1":s1,
        "2":s2
    }
    menu2 = {
        "1":t1,
        "2":t2
    }

    print(msg1,msg2)
    s_choice = input("school:")
    school_obj = menu1[s_choice]
    c_name = input("courser:")
    c_price = input("price:")
    c_period = input("period:")
    t_choice = input("teacher:")
    teacher_obj = menu2[t_choice]

    c2 = Courser(c_name,c_price,c_period,school_obj,teacher_obj)
    print("%s 属于 %s %s" %(c2.name,c2.school.name,c2.teacher.name))

 

posted @ 2019-05-07 22:35  saber゛  Views(184)  Comments(0)    收藏  举报