面向对象之组合(学生管理系统)
#--*-- coding: utf-8 --*-- class Teacher: """教师""" def __init__(self,teach_name,teach_age,teach_date,course): self.teach_name = teach_name self.teach_age = teach_age self.teach_date = teach_date self.course = course class Course: """课程""" def __init__(self,course_name,course_price,course_startdate): self.course_name = course_name self.course_price = course_price self.course_startdate = course_startdate class Student: """学生""" def __init__(self,stu_name,stu_age,course,teacher,credit): self.stu_name = stu_name self.stu_age = stu_age self.course = course self.credit = credit self.teacher = teacher def study(self,obj): print("%s study good in %s"%( self.stu_name, self.course.course_name )) def game(self): print("%s study unot very good in %s"%( self.stu_name,self.course.course_name )) class Date: """日期""" def __init__(self,start_time,end_time,course): self.start_time = start_time self.end_time = end_time self.course = course class Credit: """分数""" def __init__(self,score,course): self.score = score self.course = course course_obj = Course('python', 15800, '2017-02-19') teacher_obj = Teacher('alex','22','2017-01',course_obj) credits_obj = Credit('90',course_obj) stu1 = Student('piter',20,course_obj,teacher_obj,credits_obj ) #引入对象,方便后期可以直接调用它内部属性或者方法 stu2 = Student('tido',12,course_obj,teacher_obj,credits_obj ) go_info = [] go_info.append([{"stu_name":stu1.stu_name, "teach_name":stu1.teacher.teach_name, "course_name":stu1.course.course_name, "score":stu1.credit.score, "course_price":stu1.course.course_price },{"stu_name":stu2.stu_name, "teach_name":stu2.teacher.teach_name, "course_name":stu2.course.course_name, "score":20, #stu2.credit.score "course_price":stu2.course.course_price }]) print("学生 \t\t""老师\t\t""课程\t\t""成绩\t""课程价格") for i in go_info[0]: stuinfo = """{stu_name}\t\t{teach_name}\t\t{course_name}\t\t{score}\t\t{course_price} """.format_map(i).strip('\n') print(stuinfo) stu1.study(course_obj) stu2.game() # print(id(stu1.course.course_name)) #6670576 # print(id(course_obj.course_name)) 运行结果: 学生 老师 课程 成绩 课程价格 piter alex python 90 15800 tido alex python 20 15800