• 组合


    一,解决类与类之间代码允余问题有两种方案:  1,继承    2,组合

    1,继承:描述的是类与类之间,什么是什么的关系

    2,组合:描述的是类与类之间的关系,是一种什么有什么关系

    一个类产生的对象,该对象拥有一个属性,这个属性的值是来自另一个类的对象

     1 class Date:
     2     def __init__(self,year,mon,day):
     3         self.year = year
     4         self.mon = mon
     5         self.day = day
     6 
     7     def tell_birth(self):
     8         print('出生年月日<%s-%s-%s>' % (self.year, self.mon, self.day))
     9 
    10 class OldboyPeople:
    11     school = 'oldboy'
    12 
    13     def __init__(self, name, age, sex):
    14         self.name = name
    15         self.age = age
    16         self.sex = sex
    17 
    18 class OldboyTeacher(OldboyPeople):
    19     def __init__(self,name,age,sex,level,salary):
    20         super().__init__(name,age,sex)
    21         self.level=level
    22         self.salary=salary
    23 
    24     def change_score(self):
    25         print('teacher %s is changing score' %self.name)
    26 
    27 class Oldboystudent(OldboyPeople):
    28     def __init__(self,name,age,sex,course,):
    29         super().__init__(name,age,sex,)
    30         self.course=course
    31 
    32     def choose(self):
    33         print('student %s choose course' %self.name)
    34 
    35 
    36 tea1=OldboyTeacher('egon',18,'male',9,3.1)
    37 date_obj=Date(2000,1,1)
    38 # date_obj.tell_birth()
    39 
    40 tea1.birth=date_obj
    41 # print(tea1.birth)
    42 # tea1.birth.tell_birth()
    43 # tea1.change_score()
    44 
    45 stu1=Oldboystudent('张三',16,'male','linux')
    46 stu1.birth=Date(2002,3,3)
    47 stu1.birth.tell_birth()

    二,组合练习:

     1 class OldboyPeople:
     2     school = 'oldboy'
     3 
     4     def __init__(self, name, age, sex,):
     5         self.name = name
     6         self.age = age
     7         self.sex = sex
     8 
     9 class OldboyTeacher(OldboyPeople):
    10     def __init__(self,name,age,sex,level,salary):
    11         super().__init__(name,age,sex)
    12         self.level=level
    13         self.salary=salary
    14 
    15         self.courses=[]
    16 
    17     def change_score(self):
    18         print('teacher %s is changing score' %self.name)
    19 
    20     def tell_course_info(self):
    21         print(('老师%s 教授的课程信息如下' %self.name).center(50,'='))
    22         for course_obj in self.courses:
    23             course_obj.info()
    24 
    25 
    26 class Oldboystudent(OldboyPeople):
    27     def __init__(self,name,age,sex):
    28         super().__init__(name,age,sex,)
    29         self.courses=[]
    30 
    31     def choose(self):
    32         print('student %s choose course' %self.name)
    33 
    34     def tell_course_info(self):
    35         print(('学生%s 学习的课程信息如下' % self.name).center(50, '='))
    36         for course_obj in self.courses:
    37             course_obj.info()
    38 
    39 class Course:
    40     def __init__(self,cname,period,price):
    41         self.cname=cname
    42         self.period=period
    43         self.price=price
    44 
    45     def info(self):
    46         print('课程信息<名字:%s 周期:%s  价钱:%s>' %(self.cname,self.period,self.price))
    47 
    48 
    49 tea1=OldboyTeacher('egon',18,'male',9,3.1)
    50 stu1=Oldboystudent('张三',16,'male')
    51 
    52 python=Course('Python全栈开发','5mons',3000)
    53 linux=Course('Linux高级架构师','5mons',2000)
    54 go=Course('Go开发工程师','3mons',1000)
    55 
    56 
    57 # # 给老师添加课程
    58 # tea1.courses.append(python)
    59 # tea1.courses.append(linux)
    60 
    61 # print(tea1.courses)
    62 # tea1.courses[0].info()
    63 # for course_obj in tea1.courses:
    64 #     course_obj.info()
    65 
    66 # tea1.tell_course_info()
    67 
    68 
    69 # 给学生添加课程
    70 stu1.courses.append(python)
    71 stu1.courses.append(go)
    72 stu1.courses.append(linux)
    73 stu1.tell_course_info()
  • 相关阅读:
    微信小程序开发教程目录
    Head First设计模式之目录
    CentOS安装NodeJS
    docker镜像打包
    .net core 2.2部署到Windows Server 2012 R2 standard
    MySQL job/定时任务/event 学习
    “sgen.exe”未能运行。文件名或扩展名太长
    Linux 服务器如何设置文件和文件夹的读写权限
    添加“Git Bash Here”到右键菜单
    .Net Core中文编码问题
  • 原文地址:https://www.cnblogs.com/HZLS/p/10586193.html
Copyright © 2020-2023  润新知