继承-学校

#-*-coding:utf-8-*-
__author__ = "logan.xu"

#学校 讲师 学员

class School(object):
def __init__(self,name,addr):
self.name = name
self.addr = addr
self.students = []
self.teachers = []
self.staffs = []

def enroll(self,stu_obj):
self.students.append(stu_obj)
print("为学员%s办理注册手续"%stu_obj.name)

def hire(self,staff_obj):
self.staffs.append(staff_obj)
print("雇佣新员工%s" % staff_obj.name)

class SchoolMember(object): #严用新式类写法
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex

def tell(self):
pass




class Teacher(SchoolMember):
def __init__(self,name,age,sex,salary,course):
super(Teacher, self).__init__(name,age,sex)
self.salary = salary
self.course = course

def tell(self):
print('''
---info of Teacher:%s----
Name:%s
Age:%s
Sex:%s
Salary:%s
Course:%s
'''%(self.name,self.name,self.age
,self.sex,self.salary,self.course))

def teach(self):
print("%s is teaching course[%s]"%(self.name,self.course))

class Student(SchoolMember):
def __init__(self,name,age,sex,stu_id,grade):
super(Student, self).__init__(name,age,sex)
self.stu_id = stu_id
self.grade = grade

def tell(self):
print('''
---info of Student:%s----
Name:%s
Age:%s
Sex:%s
stu_id:%s
stu_id:%s
'''%(self.name,self.name,self.age
,self.sex,self.stu_id,self.grade))

def pay_tuition(self,amount):
print("%s has paid tuition for $%s"% (self.name,amount))


#实例化

school = School("IT","朝阳")

t1 = Teacher("boy",56,"MF",200000,"Linux")
t2 = Teacher("Alex",22,"M",2000,"PythonDevOps")

s1 = Student("Morgan",23,"MF",1001,"PythonDevOps")
s2 = Student("Lydia",19,"F",1002,"Linux")

t1.tell()
s1.tell()

school.hire(t1)
school.enroll(s1)
school.enroll(s2)

print(school.students)
print(school.staffs)

school.staffs[0].teach()

for stu in school.students:
stu.pay_tuition(5000)


>>>>>>>>>>result>>>>>>>>>>>>>

---info of Teacher:boy----
Name:boy
Age:56
Sex:MF
Salary:200000
Course:Linux

---info of Student:Morgan----
Name:Morgan
Age:23
Sex:MF
stu_id:1001
stu_id:PythonDevOps

雇佣新员工boy
为学员Morgan办理注册手续
为学员Lydia办理注册手续
[<__main__.Student object at 0x10591a588>, <__main__.Student object at 0x10591a5c0>]
[<__main__.Teacher object at 0x10591a518>]
boy is teaching course[Linux]
Morgan has paid tuition for $5000
Lydia has paid tuition for $5000

原文地址:https://www.cnblogs.com/drizzle-xu/p/8931712.html