zoukankan      html  css  js  c++  java
  • 面向对象-组合

    组合:
    学生有课程:stu.course1=python (stu 与 python都是实例化对象,course1 是变量名,可以变化)
    学生有生日:stu.brith=d (stu与d 是实例化的对象,brith是变量名,可以根据意思写其他的)
    class People:
        school = 'luffycity'
    
        def __init__(self, name, age, sex):
            self.name = name
            self.age = age
            self.sex = sex
    
    
    class Teacher(People):
    
        def __init__(self, name, age, sex, level, salary):
            # People.__init__(self, name, age, sex)
            super().__init__(name, age, sex)
    
            self.level = level
            self.salary = salary
    
        def teach(self):
            print("%s teaching " % self.name)
    
    
    class Student(People):
    
        def __init__(self, name, age, sex, class_time):
            # People.__init__(self, name, age, sex)
            super().__init__(name, age, sex)
            self.class_time = class_time
    
        def learn(self):
            print("%s learing" % self.name)
    
    
    class Course:
        def __init__(self, course_name, course_price, course_period):
            self.course_name = course_name
            self.course_price = course_price
            self.course_period = course_period
    
        def tell(self):
            print("课程名称 <%s>  课程价格 <%s>  课程时期<%s>" % (self.course_name, self.course_price, self.course_period))
    
    class Date:
        def __init__(self,year,month,day):
            self.year=year
            self.month=month
            self.day=day
    
        def tell_info(self):
            print("%s-%s-%s"%(self.year,self.month,self.day))
    
    d=Date(2018,12,10)
    s1=Student('mak',18,'male','2018/12/10')
    s1.brith=d
    s1.brith.tell_info()
    
    t1 = Teacher('alex', 28, '', 'A', '10')
    t2 = Teacher('engo', 29, '', 'A', '30')
    s1 = Student('may', '18', '', '2018/12/10')
    s2 = Student('eric', '21', '', '2018/12/10')
    
    python = Course('python', '300', '3months')
    linux = Course('linux', '400', '4months')
    
    t1.course = python  # 先增加上课程这一项
    print(t1.course.course_name)  # 再取course的细项
    print(t1.course.course_price)
    print(t1.course.course_period)
    
    t2.course=linux  #先增加上课程这一项
    print(t2.course.__dict__)  #再调取course的细项
    
    s1.course = python
    print(s1.course.course_name)
    print(s1.course.course_price)
    print(s1.course.course_period)
    
    s1.course = python # 想要组合其他项目;要先增加这项
    s2.course2=linux
    s1.course.tell()
    s2.course2.tell()
    
    s1.courses = []
    s1.courses.append(python)
    s1.courses.append(linux)
  • 相关阅读:
    windows10+vs2010+lwip+Wireshark+winpcap环境变量配置
    Anaconda中安装了Libtiff模块,但运行程序显示ModuleNotFoundError: No module named 'libtiff'
    利用Anaconda软件安装opencv模块
    Windows10+Anaconda+PyTorch(cpu版本)环境搭建
    Spyder中报错: Check failed: PyBfloat16_Type.tp_base != nullptr
    Keras中图像维度介绍
    机器学习2-7
    LeetCode637. 二叉树的层平均值
    LeetCode617. 合并二叉树
    LeetCode590. N叉树的后序遍历
  • 原文地址:https://www.cnblogs.com/hexiaorui123/p/10201407.html
Copyright © 2011-2022 走看看