简体   繁体   English

Python 将派生类合并到相同的基础 class?

[英]Python combine derived classes to the same base class?

Say I have a base class Person and two derived classes Footballer and Musician .假设我有一个基础 class Person和两个派生类FootballerMusician

Obviously a person can be both a footballer and a musician, so maybe I want a third class called FootballerAndMusician , but this class will just be a combination of the two other classes (there will not be conflicts: the two derived classes only override some specific things, and nothing in Musician overrides that which is overriden in Footballer , and vice versa).显然一个人既可以是足球运动员也可以是音乐家,所以也许我想要第三个 class 称为FootballerAndMusician ,但是这个 class 将只是其他两个类的组合(不会有冲突:两个派生类只覆盖一些特定的东西,并且Musician中的任何内容都不会覆盖Footballer中覆盖的内容,反之亦然)。

What is the appropiate way to implement this pattern in Python that avoids having to repeat the code in the two derived classes in a third class (Imagine if I had to do this with several classes…)?在 Python 中实现此模式的适当方法是什么,以避免在第三个 class 中的两个派生类中重复代码(想象一下,如果我必须对多个类执行此操作……)?

Basically, I want to implement many derived classes to Person , and then easily be able to combine them as long as there aren't conflicts.基本上,我想为Person实现许多派生类,然后只要没有冲突就可以轻松地将它们组合起来。

How is this designed in Python?这在 Python 中是如何设计的?

This is called Multiple Inheritance, and in Python this looks like this这称为多个 Inheritance,在 Python 中,它看起来像这样

class FootballerAndMusician(Footballer, Musician):
  # your specific class code goes here, like

Now that class gets access to all the methods of both of the superclasses.现在 class 可以访问两个超类的所有方法。

There's some subtle complexities with the order in which methods are looked up.查找方法的顺序有一些微妙的复杂性。 In this simple case, though, it's just "left to right".然而,在这个简单的例子中,它只是“从左到右”。 So if both Footballer and Musician have a method called play then in the example above your FootballerAndMusician would use the Footballer version.因此,如果FootballerMusician都有一个名为play的方法,那么在上面的示例中,您的FootballerAndMusician将使用Footballer版本。

Note that multiple inheritance can get complex and messy quickly, so you should really use it sparingly.请注意,多个 inheritance 会很快变得复杂和混乱,因此您应该谨慎使用它。 I would suggest avoiding it if it's just in order to create some big "taxonomy".如果只是为了创建一些大的“分类法”,我建议避免使用它。

It's quite simple:这很简单:

class FootballerAndMusician(Footballer, Musician):
    pass

This assumes, of course, that both Footballer and Musician (and Person ) are correctly designed to allow for multiple inheritance.当然,这假设FootballerMusician (以及Person )都被正确设计为允许多个 inheritance。 See https://rhettinger.wordpress.com/2011/05/26/super-considered-super/ for advice on using super correctly.有关正确使用super的建议,请参阅https://rhettinger.wordpress.com/2011/05/26/super-considered-super/

Multiple inheritance is what you're looking for.您正在寻找多个 inheritance。

Class Base1:
       (body_1)

Class Base2:
     (body_2)

Class Derived(Base1, Base2):
     (body_3)

As long as your Base classes Footballer and Musician don't interfere with the other's methods, you're good to go.只要您的 Base Class FootballerMusician不干扰其他人的方法,您就可以使用 go。
Additionally, if you need to explicitly use the constructor,此外,如果您需要显式使用构造函数,

Class Derived(Base1, Base2):
    def __init__(self, args):
        super(Derived, self).__init__(args)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM