码迷,mamicode.com
首页 > 其他好文 > 详细

类继承

时间:2018-09-07 23:56:24      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:pen   student   for   lex   _id   mount   add   ber   inux   

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

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

def hire(self,staff_obj):
print("雇佣新员工%s" % 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---
Name:%s
Age:%s
Sex:%s
Salary:%s
Course:%s
""" % (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---
Name:%s
Age:%s
Sex:%s
Stu_id:%s
Grade:%s
""" % (self.name, self.age, self.sex, self.stu_id, self.grade))
def pay_tuition(self,amount):
print("%s pay amount [%s]" % (self.name,amount))

school = School("Yangtzeu","荆州")
print(school.name,school.addr)

t1 = Teacher("Alice",55,"M",2000,"Linux")
t2 = Teacher("Alex",23,"M",3000,"python")

s1 = Student("张良",35,"M",10001,"python")
s2 = Student("李商隐",24,"F",10002,"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)

类继承

标签:pen   student   for   lex   _id   mount   add   ber   inux   

原文地址:https://www.cnblogs.com/yanyezt/p/9607551.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!