• 面向对象-组合


    组合:
    学生有课程: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)
  • 相关阅读:
    Google Shell Style Guide
    50 Must-have plugins for extending Twitter Bootstrap
    HTTP 请求头中的 X-Forwarded-For
    如何让 PowerPoint 幻灯片「高大上」?
    数据挖掘系列(1)关联规则挖掘基本概念与Aprior算法
    关于大型网站技术演进的思考(三)--存储的瓶颈(3)
    基于 Nginx XSendfile + SpringMVC 进行文件下载
    如何成为全栈工程师?
    Sqlserver通过列名查表名
    animate
  • 原文地址:https://www.cnblogs.com/hexiaorui123/p/10201407.html
Copyright © 2020-2023  润新知