zoukankan      html  css  js  c++  java
  • 派生组合示例

    class People:
        def __init__(self,name,age,sex):
            self.name=name
            self.age=age
            self.sex=sex
    class Course:
        def __init__(self,name,period,price):
            self.name=name
            self.period=period
            self.price=price
        def tell_info(self):
            print("%s %s %s"%(self.name,self.period,self.price))
    class Teacher(People):
        def __init__(self,name,age,sex,job_title):
            super(Teacher,self).__init__(name,age,sex)      #使用父类的__init__:先找到Teacher的父类,再把Teacher 的self(对象)传给父类进行初始化
            self.job_title=job_title
            self.course=[]
            self.students=[]
    class Student(People):
        def __init__(self,name,age,sex):
            super(Student,self).__init__(name=name,age=age,sex=sex)
            self.course=[]
    
    wesley=Teacher('wesley',18,'male','nb')
    print(wesley.name,wesley.age,wesley.sex,wesley.job_title)
    s1=Student('hh',15,'female')
    print(s1.name,s1.age,s1.sex)
    python=Course('python','3months',3000)
    linux=Course('linux','3months',3000)
    
    wesley.course.append(python)     #组合,wesley.course 是python这个对象
    wesley.course.append(linux)
    s1.course.append(python)
    # print(wesley.course)
    wesley.students.append(s1)
    
    for obj in wesley.course:  #obj 是python,linux 对象
        obj.tell_info()        #调用对象的方法
    wesley 18 male nb
    hh 15 female
    python 3months 3000
    linux 3months 3000
  • 相关阅读:
    错题
    static变量与普通变量的异同
    C—变量
    C—变量—register
    HDU_oj_1001 Sum Problem
    HDU_oj_1000 A+B Problem
    复变函数(上)
    信号与系统(下)
    信号与系统(中)
    信号与系统(上)
  • 原文地址:https://www.cnblogs.com/wuxi9864/p/9932961.html
Copyright © 2011-2022 走看看