组合

组合的原理:一个类的对象具备一个指向另外一个类的对象的属性

class People:
    def __init__(self, name, age, sex):
        self.name = name
        self.age = age
        self.sex = sex


class Course:
    def __init__(self, name, period, price):
        self.name = name
        self.period = period
        self.price = price

    def tell_info(self):
        print('<%s %s %s>' % (self.name, self.period, self.price))


class Teacher(People):
    def __init__(self, name, age, sex, job_title):
        People.__init__(self, name, age, sex)
        self.job_title = job_title
        self.course = []
        self.students = []


class Student(People):
    def __init__(self, name, age, sex):
        People.__init__(self, name, age, sex)
        self.course = []
        

egon = Teacher('egon', 18, 'male', '沙河霸道金牌讲师')
s1 = Student('牛榴弹', 18, 'female')

python = Course('python', '3mons', 3000.0)
linux = Course('python', '3mons', 3000.0)


#为老师egon和学生s1添加课程
egon.course.append(python)
egon.course.append(linux)
s1.course.append(python)

#为老师egon添加学生s1
egon.students.append(s1)

#使用
for obj in egon.course:
    obj.tell_info()

转载与:https://www.cnblogs.com/luck-L/p/9233609.html

上一篇:Python学习————字符串类型


下一篇:python 反射