Python 面向对象4-继承
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 = 0 10 def __init__(self,name,age,sex): 11 self.name = name 12 self.age = age 13 self.sex = sex 14 self.enroll() #注册 15 def enroll(self): 16 SchoolMember.member_numbers += 1 17 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 = course 26 self.salary = salary 27 #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 = course 35 self.tuition = tuition 36 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()