class People:
def __init__(self,name,age):
self.name = name
self.age = age
def eat(self):
print("%s is eating.."% self.name)
def sleep(self):
print("%s is sleeping.." % self.name)
def talk(self):
print("%s is talking.." % self.name)
class Man(People):
def piao(self):
print("%s is piaoing..20s..done"% self.name)
def sleep(self):
People.sleep(self) #调用父类,并为父类添加新功能
print("man is sleeping")
m1 = Man("chengronghua",18)
m1.eat()
m1.piao()
m1.sleep()
class People:
def __init__(self,name,age):
self.name = name
self.age = age
def eat(self):
print("%s is eating.."% self.name)
def sleep(self):
print("%s is sleeping.." % self.name)
def talk(self):
print("%s is talking.." % self.name)
class Man(People):
def __init__(self,name,age,money): #对父类的构造函数进行重构,添加money属性
People.__init__(self,name,age) #重写父类的属性
self.money = money #添加新的属性操作
print("%s 一出生就有%s money"%(self.name,self.money))
def piao(self):
print("%s is piaoing..20s..done"% self.name)
def sleep(self):
People.sleep(self)
print("man is sleeping")
class Woman(People):
def get_birth(self):
print("%s is born a baby.." % self.name)
m1 = Man("chengronghua",18,19)
w2 = Woman("chengrong",25)
m1.eat()
m1.piao()
m1.sleep()
w2.get_birth()
People.init(self,name,age) = super(Man,self).init(name,age)
super(Man,self).init(name,age) 新式类写法
class People: 经典类写法
class People(object): 新式类写法
多继承
class People:
def __init__(self,name,age):
self.name = name
self.age = age
def eat(self):
print("%s is eating.."% self.name)
def sleep(self):
print("%s is sleeping.." % self.name)
def talk(self):
print("%s is talking.." % 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): #用继承Relation来演示多继承
def __init__(self,name,age,money):
People.__init__(self,name,age)
self.money = money
print("%s 一出生就有%s money"%(self.name,self.money))
def piao(self):
print("%s is piaoing..20s..done"% self.name)
def sleep(self):
People.sleep(self)
print("man is sleeping")
class Woman(People,Relation): #用继承Relation来演示多继承
def get_birth(self):
print("%s is born a baby.." % self.name)
m1 = Man("chengronghua",18,19)
w2 = Woman("chengrong",25)
m1.make_friends(w2) #多继承
广度优先查询(python3)
深度优先查询(python2)
继承案例:
class School(object):
def __init__(self,name,addr):
self.name = name
self.addr = addr
self.students = []
self.teachers = []
def enroll(self,stu_obj):
print("为学员%s 办理注册手续"% stu_obj.name)
self.student.append(stu_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.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.age,self.sex,self.stu_id,self.grade)
)
def pay_tuition(self,amount):
print("%s has paid turion for $%s"%(self.name,amount))
school = School("老男孩","沙河")
t1 = Teacher("oldboy",56,"MF",2000000,"Linux")
t2 = Teacher("Alex",22,"M",3000,"PythonDevOps")
s1 = Student("chengronghua",35,"MF",1001,"PythonDevOps")
s2 = Student("chengrong",32,"M",1002,"Linux")
t1.tell()
s1.tell()