Python2 和Python3类的继承

继承

class A(object):
    def __init__(self, name):
        print('a init')
        self.name = name


class B(A):
    def __init__(self, name):
        # 第一种继承方法
        # super(B, self).__init__(name)  #  python2 & python3适用
        # super().__init__(name)  # python3 适用
        # 第二种继承方法
        A.__init__(self, name)  #  python2 & python3适用
        print('b init')

b = B('B')
print(b.name)
a = A('A')
print(a.name)

运行结果


66433-865df6285b09c954.png
image.png

猜你喜欢

转载自blog.csdn.net/weixin_33984032/article/details/87254098
今日推荐