day16 Python 类的聚合关系

"""
聚合关系:属于关联关系中的⼀种特例,侧重点是xxx和xxx聚合成xxx,各⾃有各自的
声明周期。比如电脑里有CPU, 硬盘, 内存等等;电脑挂了,CPU还是好的。还是
完整的个体

代码实现

聚合关系在代码上体现为:类A由类B聚合而成,类A包含有类B的全局对象,但类B的对象可以不在类A创建的时刻创建。

"""

class School(object):
    def __init__(self):
        self.__students = []

    def add_student(self, student):
        self.__students.append(student)


class Student(object):
    pass

>>> student = Student()
>>> school = School()
>>> school.add_student(student)
>>>

  

原文地址:https://www.cnblogs.com/fanghongbo/p/9959796.html