组合(对象1.属性=对象2)

1.组合的定义

    1.什么是组合?
        组合指的是某一个对象拥有一个数据属性,该属性的值是另外一个类的对象。
    2.为什么要用组合?
        减少代码的冗余。减少了继承的耦合度

    3.如何使用组合?

耦合度:
    耦: 莲藕 ---> 藕断丝连
    - 耦合度越高: 程序的可扩展性越低。
    - 耦合度越低: 程序的可扩展性越高。

总结:
    - 继承:
        继承是类与类的关系,子类继承父类的属性/方法,子类与父类是一种 “从属” 关系。

    - 组合:
        组合是对象与对象的关系,一个对象拥有另一个对象中的属性/方法,是一种什么有什么的关系

2.组合实现      一个对象通过添加属性为另一个对象的方式实现组合

# 组合实现
class People:
    def __init__(self, name, age, sex):
        self.name = name
        self.age = age
        self.sex = sex

# 老师类
class Teacher(People):
    def __init__(self, name, age, sex):
        super().__init__(name, age, sex)

# 学生类
class Student(People):
    def __init__(self, name, age, sex):
        super().__init__(name, age, sex)

# 日期类   另一个对象的类  直接继承的话耦合度 会很高
class Date:
    def __init__(self, year, month, day):
        self.year = year
        self.month = month
        self.day = day

    def tell_birth(self):
        print(f'''
        ===== 出生年月日 =====
            年: {self.year}
            月: {self.month}
            日: {self.day}
        ''')

stu1 = Student('HCY', 109, 'female')   对象1
date_obj = Date(1910, 11, 11)          对象2
stu1.date_obj = date_obj               添加对象2 为对象1的属性
stu1.date_obj.tell_birth()             调用对象2的方法

2.实例2

class Teacher:
    def __init__(self,name,sex,age,salary,level,ID):
        self.__name = name
        self.__sex = sex
        self.__age = age
        self.__salary = salary
        self.__level = level
        self.__ID  = ID
        self.course_list = []  将另个一个放到此对象的属性中

    def  teacher_info(self):
        print(f'''
              老师的名字:{self.__name},
              性别:{self.__age},
              年龄:{self.__sex},
              工资:{self.__salary}  
        ''')

    @property
    def  get_id (self):
        print(f'{self.__name}老师的身份证号为{self.__ID}')

    #添加课程  该功能为老师课程列表添加课程对象        
    def add_course(self,course_obj):
        return self.course_list.append(course_obj)   #将对象2追加到 对象1的属性中

    #删除指定课程 该功能删除老师课程列表中的课程对象
    def delete_course(self,course_obj):
        self.course_list.remove(course_obj)


    # 修改课程信息  修改老师课程课表中的课程
    def update_course(self,course_obj,course_name):
        if course_obj in self.course_list:
            course_obj.course_name = course_name

    #打印所有课程
    def print_course(self):
        for course_obj in self.course_list:
            course_obj.tell_courseinfo()      # 在对象1类中使用对象2类中的方法


class Course:                                                   #对象2的类方法
    def __init__(self,course_name,course_price,course_time):
        self.course_name = course_name
        self.course_price = course_price
        self.course_time = course_time
    #打印课程
    def tell_courseinfo(self):
        print(f'''
            课程名称:{self.course_name},
            课程价格:{self.course_price},
            课程周期;{self.course_time}
             '''
              )

python_obj = Course('python',100,'5mon')
linux_obj = Course('linux',1000,'5mon')
t1 = Teacher('tank','female',82,1000,'p9',1101011001)
t1.add_course(python_obj)   #  对象以数据属性传给另一个对象
t1.add_course(linux_obj)
print(t1.course_list)      
t1.print_course()           #直接调用对象1 的方法    这个方法中引用了对象2的方法  把对象2当成一个数据属性在对象1的类中使用
原文地址:https://www.cnblogs.com/bigbox/p/11946627.html