面向对象-组合

组合:
学生有课程: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)

 

posted on 2018-12-31 09:23  赫晓蕊  阅读(151)  评论(0编辑  收藏  举报

导航