zoukankan      html  css  js  c++  java
  • 面相对象编程之组合

    面相对象编程之组合

    组合指的是一个对象中,包含另一个或多个对象

    作用是: 减少代码的冗余

    -继承
     -继承是类与类的关系,子类继承父类的属性、方法,子类与父类是一种 “从属” 的关系
    -组合
     -组合是对象与对象的关系,一个对象拥有另一个对象中的属性、方法,是一种什么有什么的关系

    #案例:
    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('王畅', 24, 'female')
    date_obj = Date(1995, 4, 4 )
    stu1.date_obj = date_obj
    print(stu1.name, stu1.age, stu1.sex)
    stu1.date_obj.tell_birth(
    View Code

    联系:

    """
    练习需求:
        选课系统:
            1、有学生、老师类、学生与老师有属性:姓名、年龄、性别、课程
            2、老师与学生可以添加课程,打印学习教授课程
    """
    
    
    class People:
        def __init__(self, name, age, sex):
            self.name = name
            self.age = age
            self.sex = sex
            # 打印 出生日期
    
        def tell_birth(self):
            print(f'''
            年:{self.date_obj.year}
            月 :{self.date_obj.month}
            日 :{self.date_obj.day}
    ''')
            # 添加课程
    
        def add_course(self, course_obj):
            self.course_list.append(course_obj)
    
        def tell_all_course_info(self):
            for course_obj in self.course_list:
                course_obj.tell_course_info()
    
    
    class Student(People):
        def __init__(self, name, age, sex):
            super().__init__(name, age, sex)
            self.course_list = []
    
    
    class Teather(People):
        def __init__(self, name, age, sex):
            super().__init__(name, age, sex)
            self.course_list = []
    
    
    class Date:
        def __init__(self, year, month, day):
            self.year = year
            self.month = month
            self.day = day
    
    
    class Course:
        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_course_info(self):
            print(f'''
            =====课程信息如下=====
            课程名称:{self.course_name}
            课程价格:{self.course_price} 
            课程周期:{self.course_time}
            ''')
    #创建学生对象
    stu1 = Student('王畅', 18, 'female')
    date_obj = Date('2001', '4', '4')
    stu1.date_obj = date_obj
    stu1.tell_birth()
    
    #创建课程对象
    python_obj = Course('python', 77777, 6)
    go_obj = Course('go', 88888, 4)
    
    #当前学生添加课程对象
    stu1.add_course(python_obj)
    stu1.add_course(go_obj)
    stu1.tell_all_course_info()
    View Code
  • 相关阅读:
    自动化运维工具Ansible
    svn服务
    关于nagios系统下使用shell脚本自定义监控插件的编写以及没有实时监控图的问题
    企业级监控nagios实践
    centos6 下FastDFS 在storage节点上nginx的fastdfs-nginx-module 模块编译出现的问题
    分布式文件系统FastDFS
    运维的各个阶段
    用php做一个简单的注册用户功能
    ttttttttttt
    exclude和include当中/**和/*区别
  • 原文地址:https://www.cnblogs.com/127-2933/p/11943110.html
Copyright © 2011-2022 走看看