python入门教程之十三继承

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()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值