1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # 作者:Presley 4 # 邮箱:1209989516@qq.com 5 # 时间:2018-08-05 6 # OOP学习2 7 8 class SchoolMember(object): 9 member_numbers = 010 def __init__(self,name,age,sex):11 self.name = name12 self.age = age13 self.sex = sex14 self.enroll() #注册15 def enroll(self):16 SchoolMember.member_numbers += 117 print("The [{0}] school member [{1}] is enrolled!".format(self.member_numbers,self.name))18 19 def tell(self):20 print("Hello my name is %s"%(self.name))21 22 class Teacher(SchoolMember):23 def __init__(self,name,age,sex,course,salary): #course 课程 ,salary 薪水24 super(Teacher,self).__init__(name,age,sex)#先定义重写父类的name,age,sex这三个值,然后把父类的name,age,sex的值拿过来25 self.course = course26 self.salary = salary27 #SchoolMember.__init__(self,name,age,sex)与super方法一样28 def teaching(self):29 print("Teacher [{0}] is teaching [1]".format(self.name,self.course))30 31 class Student(SchoolMember):32 def __init__(self,name,age,sex,course,tuition): #tuition学费33 super(Student,self).__init__(name,age,sex)34 self.course = course35 self.tuition = tuition36 def pay_tution(self): #交学费37 print("cao,student [{0}] paying tution [{1}]".format(self.name,self.tuition))38 39 t1 = Teacher("Alex",22,'F',"PY",1000)40 t2 = Teacher("Tenglan",25,"N/A","PY",900)41 42 s1 = Student("SanJiang",24,"Female","python",15000)43 s2 = Student("BaoAn",23,"F","python",5000)44 45 t1.tell()46 t1.teaching()47 48 s1.pay_tution()