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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | #写一个类SchoolMember class SchoolMember( object ): member_num = 0 def __init__( self ,name,age,sex): self .name = name self .age = age self .sex = sex self .reg() # self.member_num += 1 def reg( self ): SchoolMember.member_num + = 1 print ( "SchoolMember[%s] is come to school,SchoolMember [%s] is already registered" % ( self .member_num, self .name)) def tell( self ): print ( "hello,my name is [%s],and my age is [%s]" % ( self .name, self .age)) #写一个SchollMember的子类teacher class Teacher(SchoolMember): def __init__( self ,name,age,sex,course,salary): super (Teacher, self ).__init__(name,age,sex) self .course = course self .salary = salary def teaching( self ): print ( "Teacher [%s] is teaching [%s]" % ( self .name, self .course)) class Student(SchoolMember): def __init__( self ,name,age,sex,course,xufei): super (Student, self ).__init__(name,age,sex) self .course = course self .xufei = xufei def pay_money( self ): print ( "student [%s] must pay [%s] to school" % ( self .name, self .xufei)) #首先分别创建2个teacher的实例和2个student的实例 print ( "首先分别创建2个teacher的实例和2个student的实例" ) t1 = Teacher( "teacher1" , "12" , "M" , "python" , "123" ) t2 = Teacher( "teacher2" , "13" , "M" , "shell" , "234" ) s1 = Student( "Student1" , "1" , "W" , "python" , "1234" ) s2 = Student( "Student2" , "2" , "M" , "shell" , "12345" ) # 首先分别创建2个teacher的实例和2个student的实例 # SchoolMember[1] is come to school,SchoolMember [teacher1] is already registered # SchoolMember[2] is come to school,SchoolMember [teacher2] is already registered # SchoolMember[3] is come to school,SchoolMember [Student1] is already registered # SchoolMember[4] is come to school,SchoolMember [Student2] is already registered print ( "teacher调用父类SchoolMember的方法tell" ) t1.tell() t2.tell() # teacher调用父类SchoolMember的方法tell # hello,my name is [teacher1],and my age is [12] # hello,my name is [teacher2],and my age is [13] print ( "teacher实例调用teacher类的方法" ) t1.teaching() t2.teaching() # teacher实例调用teacher类的方法 # Teacher [teacher1] is teaching [python] # Teacher [teacher2] is teaching [shell] print ( "student调用父类SchoolMember的方法tell" ) s1.tell() s2.tell() # student调用父类SchoolMember的方法tell # hello,my name is [Student1],and my age is [1] # hello,my name is [Student2],and my age is [2] print ( "student实例调用student类的方法" ) s1.pay_money() s2.pay_money() # student实例调用student类的方法 # student [Student1] must pay [1234] to school # student [Student2] must pay [12345] to school |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步