# class People:#经典类#父类
class People(object): #新式类 # 父类
def __init__(self,name,age):
self.name = name
self.age = age
def __del__(self):
pass
def eat(self):
print('%s is eating...'%self.name)
def talk(self):
print("%s is talking ..."%self.name)
def sleep(self):
print("%s is sleeping..."%self.name)
class Relation(object):
def make_friends(self,obj):
print('%s is making friends with %s'%(self.name,obj.name))
class Man(People,Relation):#多继承
def __init__(self,name,age,money):
# People.__init__(self,name,age)
super(Man,self).__init__(name,age)#新式类的写法
self.money = money
print('%s 一出生就有%s'%(self.name,self.money))
def piao(self):
print("%s is piaoing....20s ...done."%self.name)
def sleep(self):#更改父类方法
People.sleep(self)#d调用父类的方法
print("man is sleeping")
class Woman(People,Relation):
def get_brith(self):
print('%s is born a baby ...'%self.name)
m1 = Man("test",22,200)
# m1.eat()
# m1.piao()
# m1.sleep()
w1 = Woman('luxi',25)
# w1.get_brith()
m1.make_friends(w1)
#多继承析构函数会继承最前面的类
#python3经典类和新式类都是按广度优先继承的
#python2经典类深度优先来继承的,新式类按广度优先继承
#继承实例
class School (object):
def __init__(self,name,addr):
self.name = name
self.addr = addr
self.students = []
self.staffas = []
def enroll(self,stu_obj):
print("为学员%s办理注册手续"%stu_obj.name)
self.students.append(stu_obj)
def hire(self,staff_obj):
self.staffas.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
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("old boy","北京")
t1 = Teacher("Oldboytest",'56','MF',2000000,"Linux")
t2 = Teacher("Mrzhang",'22','M',2000,"python3")
s1= Student("小刘",22,"MF",10001,"python3")
s2= Student("小解",19,"MF",10002,"Linux")
t1.tell()
s1.tell()
school.hire(t1)
school.enroll(s1)
school.enroll(s2)
print(school.students)
print(school.staffas)
school.staffas[0].teach()
for stu in school.students:
stu.pay_tuition(5000)