面向对象之组合(学生管理系统)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | #--*-- 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 |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步