首页 > 编程语言 > 详细

python多继承

时间:2015-06-05 21:15:35      阅读:267      评论:0      收藏:0      [点我收藏+]

http://blog.csdn.net/pipisorry/article/details/46381341

There are two typical use cases forsuper:

In a class hierarchy withsingle inheritance, super can be used to refer to parent classes withoutnaming them explicitly, thus making the code more maintainable. This useclosely parallels the use ofsuper in other programming languages.

The second use case is to support cooperative multiple inheritance in adynamic execution environment. This use case is unique to Python and isnot found in statically compiled languages or languages that only supportsingle inheritance. This makes it possible to implement “diamond diagrams”where multiple base classes implement the same method. Good design dictatesthat this method have the same calling signature in every case (because theorder of calls is determined at runtime, because that order adaptsto changes in the class hierarchy, and because that order can includesibling classes that are unknown prior to runtime).

For both use cases,a typical superclass call looks like this:
class C(B):
    def method(self, arg):
        super().method(arg)    # This does the same thing as:super(C, self).method(arg)

[super]


类继承实例

父类定义:

class Parent1(object):
    def on_start(self):
        print(‘do something‘)

class Parent2(object):
    def on_start(self):
        print(‘do something else‘)

class Parent3(object):
    pass
子类定义1:

class Child(Parent1, Parent2, Parent3):
    def on_start(self):
        for base in Child.__bases__:
            try:
                base.on_start(self)
            except AttributeError:
                # handle that one of those does not have that method
                print(‘"{}" does not have an "on_start"‘.format(base.__name__))
Child().on_start()  # c = Child(); c.on_start()


结果输出:
do something
do something else
"Parent3" does not have an "on_start"
子类定义2

class Child(Parent1, Parent2):
    def on_start(self):
        super(Child, self).on_start()
        super(Parent1, self).on_start()

class Child(Parent1, Parent2):
    def on_start(self):
        Parent1.on_start(self)
        Parent2.on_start(self)

Child().on_start()  # c = Child(); c.on_start()

两个结果都输出为:
do something
do something else

Note:

1. since both of the parents implements the same method, super will just be the same as the first parent inherited, from left to right (for your code,Parent1). Calling two functions with super is impossible.

2. 注意子类定义2中的用法1

[Python Multiple Inheritance: call super on all]


注意的问题

按类名访问就相当于C语言之前的GOTO语句...乱跳,然后再用super按顺序访问,就有问题了。

所以建议就是要么一直用super,要么一直用按照类名访问

最佳实现:

  1. 避免多重继承
  2. super使用一致
  3. 不要混用经典类和新式类
  4. 调用父类的时候注意检查类层次
[Python类继承的高级特性]

from:http://blog.csdn.net/pipisorry/article/details/46381341

ref:Python 为什么要继承 object 类?


python多继承

原文:http://blog.csdn.net/pipisorry/article/details/46381341

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!