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)
  • 相关阅读:
    51nod 1412 AVL树的种类
    bzoj1093 [ZJOI2007]最大半联通子图 缩点 + 拓扑序
    bzoj1116 [POI2008]CLO 边双联通分量
    luoguP4366 [Code+#4]最短路 最短路
    51nod1821 最优集合 贪心
    51nod2000 四边形分割平面 规律题
    luoguP3250 [HNOI2016]网络 树链剖分 + 堆
    [Luogu5162]WD与积木(多项式求逆)
    [Luogu5161]WD与数列(后缀数组/后缀自动机+线段树合并)
    [Luogu5106]dkw的lcm
  • 原文地址:https://www.cnblogs.com/hexiaorui123/p/10201407.html
Copyright © 2011-2022 走看看