美文网首页
关于python的继承问题

关于python的继承问题

作者: 断剑长生 | 来源:发表于2018-11-23 11:55 被阅读0次

1、python的多继承中,如果父类拥有相同的方法和属性,以优先继承为主

代码如下:

```

class A(object):

text ='hello a'

    def print_hello(self):

print('this is print_hello of A class')

print(self.text)

class B(object):

text ='hello b'

    def print_hello(self):

print('this is print_hello of B class')

print(self.text)

class C(B, A):

pass

def inherit_test():

c = C()

c.print_hello()

if __name__ =='__main__':

inherit_test()

```

运行结果如下:

```

this is print_hello of B class

hello b

```

相关文章

网友评论

      本文标题:关于python的继承问题

      本文链接:https://www.haomeiwen.com/subject/ehfjqqtx.html