类练习2


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

def enroll(self,stu_obj):
print('for %s do enrolling' %stu_obj.name)
self.students.append(stu_obj)
def hire(self,staff_obj):
print('for %s do hiring' %staff_obj.name)

self.staffs.append(staff_obj)
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
grade: %s
''' % (self.name, self.name, self.age, self.sex, self.stu_id, self.grade))
def pay_tuition(self,amount):
print('%s has paid tution for $%s '%(self.name,amount) )

school = School('yunnanu','kunming')
t1 =Teacher('lian',33,'M',20000,"Linux")
t2 =Teacher('Alex',22,"F",3000,"PythonDevOps")
s1 = Student("ChenRonghua",22,"M",'2018001','PythonDevOps')
s2 = Student("xuliang",23,"M",'2018002','Linux')
t1.tell()
s1.tell()
school.enroll(s1)
school.enroll(s2)
school.hire(t1)
print(school.students[0].name)
print(school.staffs[0].name)
school.staffs[0].teach()

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


#================================

------info of Teacher : lian -----------
name:lian
age: 33
sex: M
salary: 20000
course: Linux

------info of Student : ChenRonghua -----------
name:ChenRonghua
age: 22
sex: M
stu_id: 2018001
grade: PythonDevOps

for ChenRonghua do enrolling
for xuliang do enrolling
for lian do hiring
ChenRonghua
lian
lian is teaching course [Linux]
ChenRonghua has paid tution for $1200
xuliang has paid tution for $1200

原文地址:https://www.cnblogs.com/rongye/p/9958459.html