一、定义

python面对对象的继承指的是多个类之间的所属关系,即子类默认继承父类的所由属性和方法

class A(object):
    def __init__(self):
        self.num=1
    def info_print(self):
        print(self.num)

#子类B
class B(A):
    pass

resoult=B()
resoult.info_print()  #1

二、单继承和多继承

  • 单继承:
  • 多继承:一个类同时继承多个父类

P.s:当一个类有多个父类得时候,同名属性和方法默认使用第一个父类的。

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
class A(object):
    def __init__(self):
        self.str = 'A类的初始定义'

    def print_str(self):
        print(f'运用{self.str}')

class B(object):
    def __init__(self):
        self.str = 'B类的初始定义'

    def print_str(self):
        print(f'运用{self.str}')

class C(A, B):
    pass

class D(B, A):
    pass

c = C()
c.print_str()  # 运用A类的初始定义
d = D()
d.print_str()  # 运用B类的初始定义

三、子类重写父类同名方法和属性

子类和父类具有同名属性和方法,默认使用子类的同名属性和方法

class A(object):
    def __init__(self):
        self.str = 'A类的初始定义'

    def print_str(self):
        print(f'运用{self.str}')

class B(object):
    def __init__(self):
        self.str = '子类B类的初始定义'

    def print_str(self):
        print(f'运用{self.str}')

b = B()
b.print_str()  # 运用子类B类的初始定义

四、子类调用父类的同名方法和属性

class A(object):
    def __init__(self):
        self.str = 'A类的初始定义'

    def print_str(self):
        print(f'运用{self.str}')

class B(object):
    def __init__(self):
        self.str = 'B类的初始定义'

    def print_str(self):
        print(f'运用{self.str}')

class C(A, B):
    def __init__(self):
        self.str = '子类C类的初始定义'

    def print_str(self):
        print(f'运用{self.str}')

    def print_self_str(self):
        self.__init__()
        self.print_str()
        # self即函数本身,这里前面调用了self参数,括号里就不用写了
        # self.print_str(self) 传了两遍参数会报错

    def print_A_str(self):
        A.__init__(self)
        A.print_str(self)

    def print_B_str(self):
        B.__init__(self)
        B.print_str(self)

c = C()
c.print_str() # 运用子类C类的初始定义
c.print_self_str()  # 运用子类C类的初始定义
c.print_A_str()  # 运用A类的初始定义
c.print_B_str()  # 运用B类的初始定义
c.print_str()  # 运用B类的初始定义
c.print_self_str()  # 运用子类C类的初始定义 # 前后两个print_str()输出值不一样,因为该函数没有重新初始化self.str

# __mro__查看继承顺序
print(C.__mro__)  
# (<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)

五、多层继承

eg:C类继承B类,B类继承A类,像这种三代及以上的叫多层继承。规则同继承

六、super调用父类方法

使用super()可以自动查找父类。调用顺序遵循__mro__类属性的顺序。比较适合单继承使用。

class A(object):
    def __init__(self):
        self.str='A类的初始定义'
    def print_str(self):
        print(f'运用{self.str}')

class B(A):
    def __init__(self):
        self.str = 'B类的初始定义'

    def print_str(self):
        # 1、 super()带参数
        # super(B, self).__init__()
        # super(B, self).print_str()
        # 2、 super()无参数
        super().__init__()
        super().print_str()

class C(B):
    def __init__(self):
        self.str= 'C类的初始定义'

    def print_str(self):
        # 1、 super()带参数
        # super(C,self).__init__()
        # super(C,self).print_str()
        # 2、 super()无参数
        super().__init__()
        super().print_str()

c = C()
c.print_str()  # 运用A类的初始定义

七、私有权限

  • 不能继承给子类的属性和方法需要添加私有权限
  • 私有权限不能在外部直接输出或使用
  • 语法:私有属性和方法区别在名字前面加两个下划线
class 类名():
    #私有属性
    __属性名 =# 私有方法
    def  __函数名(self):
       代码

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄