zoukankan      html  css  js  c++  java
  • 028.Python面向对象继承(单继承,多继承,super,菱形继承)


    一 继承的概念

    种类

    • 单继承
    • 多继承

    至少两个类:

    • 子类:一个类继承另外一个类,那么该类是子类(也叫作衍生类)
    • 父类:另外一个,这个被继承的类,叫做父类(也叫作超类),object 在python中 这个类是所有类的父类

    二  单继承

    2.1 子父继承之后,子类可以使用父类的公有成员属性方法

    复制代码
    class Plane():
            capitain = "一般是男性"
            price = "一般保密"
            def fly(self):
                    print("飞机都会飞")
            def capitain(self):
                    print("一般都会有一个副驾驶员")
            def __price(self):
                    print ("保密")
    # 想让一个类继承另外一个类,语法在定义类的时候,括号里面写(父类)
    class Fighter(Plane):
            pass
    
    obj = Fighter()
    print (obj.price)
    
    class Airliner(Plane):
            def Airliner_price():
                    self.__price
    复制代码

    执行

    [root@node10 python]# python3 test.py
    一般保密

    2.2 子父继承之后,子类不能调用父类的私有成员属性方法

    复制代码
    class Plane():
            capitain = "一般是男性"
            price = "一般保密"
            def fly(self):
                    print("飞机都会飞")
            def capitain(self):
                    print("一般都会有一个副驾驶员")
            def __price(self):
                    print ("保密")
    
    class Fighter(Plane):
            pass
    
    obj = Fighter()
    print (obj.price)
    
    class Airliner(Plane):
            def Airliner_price():
                    self.__price
    
    obj = Airliner()
    obj.Airliner_price()
    复制代码

    执行

    调用私有方法

    复制代码
    class Plane():
            capitain = "一般是男性"
            price = "一般保密"
            def fly(self):
                    print("飞机都会飞")
            def capitain(self):
                    print("一般都会有一个副驾驶员")
            def __price(self):
                    print ("保密")
    
    class Fighter(Plane):
            pass
    
    obj = Fighter()
    print (obj.price)
    
    class Airliner(Plane):
            def Airliner_price():
                    self.__price
    
    obj = Airliner()
    obj.__price
    复制代码

    执行

    复制代码
    [root@node10 python]# python3 test.py
    一般保密
    Traceback (most recent call last):
      File "test.py", line 22, in <module>
        obj.__price
    AttributeError: 'Airliner' object has no attribute '__price'
    复制代码

     2.3 子父继承之后,子类可以改写父类的公有方法

    • 如果子类当中含有该方法,那么优先调用子类的方法
    • 如果子类当中不含有该方法,再去调用父类的方法.

    有就调用自己的,没有就调用父类的(一定是共有的成员属性方法;)

    复制代码
    class Plane():
            capitain = "一般是男性"
            price = "一般保密"
            def fly(self):
                    print("飞机都会飞")
            def capitain(self):
                    print("一般都会有一个副驾驶员")
            def __price(self):
                    print ("保密")
    
    class Fighter(Plane):
            def capitain(self):
                    print ("机长是John")
    obj = Fighter()
    obj.capitain()
    复制代码

    执行

    [root@node10 python]# python3 test.py
    机长是John

    三  多继承

    3.1 基本结构

    复制代码
    class Picture():
            p_style = "classic"
            def colour(self):
                    print ("bule,green,gray,yellow")
    
    class Music():
            m_style = "confitable"
            def Tone(self):
                    print ("宫商角微羽")
    #继承两个父类
    class Surroundings(Picture,Music):
            pass
    obj = Surroundings()
    print (obj.p_style)
    obj.Tone()
    复制代码

    执行

    [root@node10 python]# python3 test.py
    classic
    宫商角微羽

    3.2 super 调用父类的相关公有属性方法

    1. super本身是一个类 super()是一个对象 用于调用父类的绑定方法
    2. super() 只应用在绑定方法中,默认自动传递self对象 (前提:super所在作用域存在self)
    3. super用途: 解决复杂的多继承调用顺序
    复制代码
    class Picture():
            p_style = "classic"
            def colour(self):
                    print ("bule,green,gray,yellow")
    
    class Music():
            m_style = "confitable"
            def Tone(self):
                    print ("宫商角微羽")
    
    class Surroundings(Picture,Music):
            p_style = "Modern"
            def s_tone(self):
                    print(self.p_style)
                    self.Tone()
            print (Picture.p_style)
            Music.Tone("123")
            def s_tone2(self):
    
                    res = super().p_style
                    print (res)
                    super().Tone()
    
    obj = Surroundings()
    obj.s_tone()
    复制代码

    执行

    [root@node10 python]# python3 test.py
    classic
    宫商角微羽
    Modern
    宫商角微羽

    使用super

    复制代码
    class Picture():
            p_style = "classic"
            def colour(self):
                    print ("bule,green,gray,yellow")
    
    class Music():
            m_style = "confitable"
            def Tone(self):
                    print ("宫商角微羽")
    
    class Surroundings(Picture,Music):
            p_style = "Modern"
            def s_tone(self):
                    print(self.p_style)
                    self.Tone()
            print (Picture.p_style)
            Music.Tone("123")
            def s_tone2(self):
    
                    res = super().p_style
                    print (res)
                    super().Tone()
    
    obj = Surroundings()
    obj.s_tone2()
    复制代码

    执行

    [root@node10 python]# python3 test.py
    classic
    宫商角微羽
    classic
    宫商角微羽

    四 菱形继承

    4.1 基本实例

    复制代码
    class Human():
            pty = 111
            def feelT(self):
                    print("1111")
                    print(self.pty)
                    print("2222")
    
    
    class Man(Human):
            pty = 222
            def feelT(self):
                    print("3333")
                    super().feelT()
                    print("4444")
    class Woman(Human):
            pty = 333
            def feelT(self):
                    print("5555")
                    super().feelT()
                    print("6666")
    class Children(Man,Woman):
            pty = 444
            def feelT(self):
                    print("7777")
                    super().feelT()
                    print("8888")
    
    obj = Children()
    obj.feelT()
    '''
    # mro列表  类.mro() 使用c3算法,针对于多继承的情况,按照这个列表依次调用.调用顺序都在其中
    # 如果出现重名方法,  super() 就是按照这个列表依次调用
    '''
    res = Children.mro()
    print(res)
    复制代码

    执行

    复制代码
    7777
    3333
    5555
    1111
    444
    2222
    6666
    4444
    8888
    [
    <class '__main__.Children'>,
    <class '__main__.Man'>,
    <class '__main__.Woman'>,
    <class '__main__.Human'>,
    <class 'object'>
    ]
    复制代码

    执行过程

    1. 调用obj.feelT(),这个是children的这个类,打印出7777
    2. 执行children类中方法的super().feelT(),但是每一个类都有FeelT的方法,使用Mro表(Children,man,woman,human),依次执行3333,5555,1111,但是停留在human的这个类上
    3. 执行print(self.pty),最初的是在children,打印出444
    4. 执行print("2222"),打印2222,这个类执行完,回到上一层的类woman
    5. 执行print("6666"),打印6666,则woman这个泪执行玩,回到上一层man
    6. 执行print("4444"),打印4444,则man的这个类执行完,回到上一层children
    7. 执行print("8888"),打印8888,整个过程执行,最后打印mro表

    4.2 判断子父关系

    复制代码
    class Human():
            pty = 111
            def feelT(self):
                    print("1111")
                    print(self.pty)
                    print("2222")
    
    
    class Man(Human):
            pty = 222
            def feelT(self):
                    print("3333")
                    super().feelT()
                    print("4444")
    
    class Woman(Human):
            pty = 333
            def feelT(self):
                    print("5555")
                    super().feelT()
                    print("6666")
    
    class Children(Man,Woman):
            pty = 444
            def feelT(self):
                    print("7777")
                    super().feelT()
                    print("8888")
    
    obj = Children()
    obj.feelT()
    '''
    # mro列表  类.mro() 使用c3算法,针对于多继承的情况,按照这个列表依次调用.调用顺序都在其中
    # 如果出现重名方法,  super() 就是按照这个列表依次调用
    '''
    res = Children.mro()
    print(res)
    
    res= issubclass(Children,Man)
    # 判断Children 是不是元组当中一个类的子类,有一个成立,返回真,一个都不满足,返回假
    res = issubclass(Children,(Man,Woman))
    print(res)
    # 只要有血缘关系即可.
    res = issubclass(Children,Human)
    print(res)
    
    
    # 判断obj这个对象类型是不是Children (有继承的血缘关系即可)
    '''
    python当中,万物皆是对象,只是对象常常这两字被省略.
    '''
    res = isinstance(obj,Children)
    print(res)
    res = isinstance(obj,Human)
    print(res)
    res = isinstance(obj,(Man,Woman))
    print(res)
    复制代码

    执行

    复制代码
    7777
    3333
    5555
    1111
    444
    2222
    6666
    4444
    8888
    [<class '__main__.Children'>, <class '__main__.Man'>, <class '__main__.Woman'>, <class '__main__.Human'>, <class 'object'>]
    True
    True
    True
    True
    True
    复制代码
    学习记录,小白一枚
  • 相关阅读:
    Codeforces Round #276 (Div. 1) E. Sign on Fence 二分+主席树
    Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s
    HDU 5918 Sequence I KMP
    HDU 5919 Sequence II 主席树
    hdu 5833 Zhu and 772002 高斯消元
    Codeforces Round #143 (Div. 2) E. Cactus 无向图缩环+LCA
    codeforces 45C C. Dancing Lessons STL
    deeplab hole algorithm
    有时候只是担心多余
    lstm
  • 原文地址:https://www.cnblogs.com/wangsirde0428/p/14322558.html
Copyright © 2011-2022 走看看