标签:
继承 案例 直接看代码
#!/usr/bin/env python #_*_ coding:utf-8 _* __author__ = ‘Administrator‘ #继承 class SchoolMember(object): member_nums = 0 def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex self.enroll() #self.tell() def enroll(self): SchoolMember.member_nums += 1 print("\033[32mThe [%s] school member [%s] is enrolled\033[0m"% (self.member_nums,self.name)) def tell(self): print("Hello my name is %s" % self.name) 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,tution): super(Student,self).__init__(name,age,sex) self.course = course self.tution = tution def pay_tution(self): print("cao,student [%s] paying tution [%s]"% (self.name,self.tution)) t1 = Teacher("yefei",25,"F","PY",1000) t2 = Teacher("wusiqi",23,"F","PY",1500) s1 = Student("lisi",22,"F","python",15000) s2 = Student("zhangsan",23,"F","python",5000) t1.tell() t1.teaching() s1.tell() s1.pay_tution()
标签:
原文地址:http://www.cnblogs.com/yexiaochong/p/5495804.html