关于python的继承问题

2018-11-23  本文已影响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

```

上一篇 下一篇

猜你喜欢

热点阅读