类与类的组合 小练习 选课

 编写类完成以下的嵌套关系 :

 

嵌套关系如下 :

class School:
    def __init__(self, address):
        self.address = address


bj = School('北京校区')
sh = School('上海校区')
sz = School('深圳校区')

class Course(object):
    def __init__(self, name, period, price, school=None): # school可以默认为None,
        self.name = name
        self.period = period
        self.price = price
        self.school = school   # 也可以在此处设置为None


py1 = Course('Python全栈', 110, 19999, bj)
py2 = Course('Python全栈', 110, 19999, sh)
py3 = Course('Python全栈', 110, 19999, sz)

l1 = Course('Linux运维', 110, 19999, bj)
l2 = Course('Linux运维', 110, 19999, sh)

g1 = Course('Go开发', 119, 19999, bj)


class Grade(object):
    def __init__(self, name, people, introduce, course=None):
        self.name = name
        self.people = people
        self.introduce = introduce
        self.course = course

gr1 = Grade('全栈1期',20,'....',py1)
gr2 = Grade('全栈1期',20,'....',py2)
gr3 = Grade('Linux8期',20,'....',l2)

print(gr1.people)
print(gr1.course.price)
print(gr1.course.school.address)

 

posted @ 2018-08-29 15:30  葡萄想柠檬  Views(191)  Comments(0)    收藏  举报
目录代码