Python面向对象之继承02

多继承:一个类继承多个类
单继承:一个类继承一个类

案例1:

class Base(object):
    def __init__(self,name,age):
        self.name = name
        self.age = age

    def play(self):
        print("Base play方法被调用!")

class A(Base):
    #ctrl+o :快速生成构造方法等其他方法(构造方法包含父类super)
    def __init__(self, name, age,gender):
        super().__init__(name, age)
        self.gender = gender


    # def __init__(self,name,age,gender):
    #     super(A, self).__init__(name,age)
    #     self.gender = gender


    def play(self):
        print("A play方法被调用")

class B(A):
    def play(self):
        #通过super()访问父类被覆盖的方法
        super().play()
        print("B play方法被调用")

a = A("tom",18,"男")
print(a.name)

# b = B()
# b.play()

案例2:

class Base(object):
    def __init__(self,name,age):
        self.name = name
        self.age = age

    def play(self):
        print("Base play方法被调用!")

class A(Base):
    #ctrl+o :快速生成构造方法等其他方法(构造方法包含父类super)
    def __init__(self, name, age,gender):
        super().__init__(name, age)
        self.gender = gender
    def play(self):
        print("A play方法被调用")

class B(Base):
    def play(self):
        #通过super()访问父类被覆盖的方法
        super().play()
        print("B play方法被调用")

#多继承
class C(B,A):
    pass

c = C("tom",12,"男")
# print(c.name)
c.play()#A play方法被调用
#多继承中,最先访问到的父类方法会被最先调用
#python
#经典类:支持深度优先继承
#新式类:支持广度优先继承

案例3:

class Person:
    def eat(self):
        print("人吃饭")

class Student:
    def study(self):
        print("学生学习行为")

class Child:
    def respect(self):
        print("孝顺父母的行为")

class Husband:
    def love(self):
        print("疼爱妻子的行为")

class Worker:
    def work(self):
        print("工作上班的行为")

#多继承:可扩展的功能更多,可扩展性更强
class People(Person,Student,Child,Husband,Worker):
    pass

tom = People()
tom.eat()
tom.study()
tom.respect()
tom.love()
tom.work()
"""
输出结果:
人吃饭
学生学习行为
孝顺父母的行为
疼爱妻子的行为
工作上班的行为
"""

案例4:

class Person:
    def __init__(self,name,age):
        self.name = name
        self.age = age

    def walk(self):
        print("person类的walk行为")

    def play(self):
        print("person类的play行为")

class Hero(Person):
    def walk(self):
        print("Hero类的walk行为")

    def saveWorld(self):
        print("Hero类型的saveWorld行为")

class Animal:
    def eat(self):
        print("Animal 类的eat方法")

    def play(self):
        print("Animal类的play行为")

class Pet(Hero,Animal):
    def eat(self):
        print("Pet类型 eat方法")


class Cat(Pet):
    pass

tom_cat = Cat("tom",12)
tom_cat.eat()#Pet类型 eat方法
tom_cat.walk()#Hero类的walk行为
tom_cat.play()#person类的play行为
#Cat-->Pet-->Hero-->:
#1.Cat-->Pet-->Hero-->:Person-->Person.play()#深度优先 经典类 2.0

#Cat-->Pet-->Hero-->:Animal-->Animal.play()#广度优先 新式类  3.0

# mro()  method resloution order
print(Cat.mro())#通过类名.mro()可以查看类的继承顺序
MRO:Method Relation Order(方法关系列表)
MRO生成原则:
1.子类永远在父类的前面
2.同一等级的类,按照子类中的继承顺序摆放
3.先子类,后父类的顺序原则,最终的类是Object类

super()在调用时,并不是查找父类,而是去MRO列表上找上一个类
super()在调用时,会自动把当前self传入到上一级的类的方法中

# print(Cat.__mro__)
#[<class '__main__.Cat'>,
# <class '__main__.Pet'>,
# <class '__main__.Hero'>,
# <class '__main__.Animal'>,
# <class '__main__.Person'>,
# <class 'object'>]


"""
深度优先:
{经典类}
python2中,类型声明没有继承自object语法

先查询自己的第一个父类中是否拥有自己需要的属性/方法,
如果没有的情况下,直接查询该父类的父类中是否拥有,如果没有
查询当前类型的第二个,依次类推...

广度优先:
{新式类}
先查询自己继承的所有父类中是否拥有需要的属性/方法,
如果都没有的情况下继续查询父类的父类中是否有需要的
属性/方法。(同级继承关系)
"""

案例5:

"""
多继承之菱形继承
   A
B     C
   D
"""
# class Person():
#     num = 1
#     def play(self):
#         print("人类诞生了.....")
#
# class Man(Person):
#     num = 2
#     def play(self):
#         super().play()
#         print("男士出场....")
#
# class Woman(Person):
#     num = 3
#     def play(self):
#         super().play()
#         print("女士出场....")
#
# class Child(Man,Woman):
#     num = 4
#     def play(self):
#         super().play()
#         print("小孩出场....")

# c = Child()
# c.play()

"""
继承顺序:Child-->Man-->Woman-->Person
人类诞生了.....
女士出场....
男士出场....
小孩出场....
"""

class Person():
    num = 1
    def play(self):
        print(self.num)
        print("人类诞生了.....")

class Man(Person):
    num = 2
    def play(self):
        super().play()
        print(super().num)
        print(self.num)
        print("男士出场....")

class Woman(Person):
    num = 3
    def play(self):
        super().play()
        print(super().num)
        print(self.num)
        print("女士出场....")

class Child(Man,Woman):
    num = 4
    def play(self):
        super().play()
        print(super().num)
        print(self.num)
        print("小孩出场....")

c = Child()
c.play()

print(Child.mro())
# [<class '__main__.Child'>, <class '__main__.Man'>,
# <class '__main__.Woman'>, <class '__main__.Person'>, <class 'object'>]
#继承顺序:Child-->Man-->Woman-->Person
"""
继承顺序:Child-->Man-->Woman-->Person

super()
    使用super去调用父级的方法时,实际是在用super调用MRO列表中的上一级中的方法,
    使用super去访问父级的属性时,实际是在用super访问MRO列表中的上一级中的属性。
    
super()本身调用父级方法时,传递的self对象,就是这个方法中的那个self对象自己

4
人类诞生了.....
1
4
女士出场....
3
4
男士出场....
2
4
小孩出场....
"""
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值