class Schoolmember (object): #定义学校
member=0# Default member is 0
amount=0# Default tuition fee is 0 yuan
def __init__ (self,name,age,sex): #构造函数, defining the properties of the parent class
Self.name=name
Self.age=age
Self.sex=sex
self.enroll () #调用注册的函数
def enroll (self):
' Register '
print ("Just is enrolled a new school member[%s]"% self.name)
schoolmember.member+=1# Each member of the register, the number of members plus 1
def tell (self): #用来获取成员的属性
Print ("-----info:%s----"%self.name)
For k,v in Self.__dict__.items (): #用字典的形式来获取成员的属性
print ("\ T", k,v)
print ("------End-------")
class Teacher (schoolmember): #定义一个老师的类, and inherit this school
def __init__ (self,name,age,sex,salary,course): #继承父类并且重构
schoolmember.__init__ (self,name,age,sex) #继承父类
self.salary=salary
Self.course=course
def Teaching (self):
print ("The teacher is [%s] and he course is [%s]"% (self.name,self.course))
class Student (schoolmember): #定义一个学生的类来继承父类
def __init__ (self,name,age,sex,course,tuition):
schoolmember.__init__ (self,name,age,sex)
Self.course=course
self.tuition=tuition
def pay_tuition (self,amount): #学费
print ("Student%s has justed paied%s"% (Self.name,amount))
Self.amount+=amount
t1=teacher ("Xiangshucai", Si, "F", 4500, "physical")
s1=student ("Xiangxiao", "F", "Python", 6500)
s2=student ("Liuhaimei", 23°c, "F", "Python15", 11000)
t1.teaching ()
T1.tell ()
S1.tell ()
The results are as follows:
Instances of Python inheritance