Python: 多级继承

Python: multi level inheritance

我无法找到 Python 继承问题的有效解决方案。

所以存在如下代码:

class Foo( object ):
        def __init__():
                self.some_vars
        def foo_func():
                x = Bar()
        def foo_bar_func()
                x += 1

class Fighters( Foo ):
        def __init__():
                Foo.__init__()
        def func1():
                Foo.some_attribute
        def func2():
                Foo.someother_func()

class Bar():
        def bar_func():
                #some stuff here

问题在于我需要覆盖 Bar.bar_func() 但那是两层深。我通过执行以下操作解决了它:

class Foo( Foo ):
        def __init__():
                Foo.__init__()
        def foo_func():          #Overridden method
                x = myBar()   

class myFighters( Foo ):
        def __init__():
                Foo.__init__()
        def func1():
                Foo.some_attribute
        def func2():
                Foo.someother_func()

class myBar():
        def bar_func():      #the function that I actually need to change
                #my sweet code here

唯一真正不同的是 myBar.bar_func() 但我必须至少做 2 件我认为丑陋的事情。

一个是我必须创建一个继承自 Fooclass Foo。这似乎是一件奇怪的事情,并且不会让事情变得非常清楚。我这样做是为了避免将 myFighters 中的每个引用从 Foo 重命名为 myFoo

第二个是我必须将所有代码从 Fighters 复制到 myFighters 中,唯一的目的是使用 Bar() 中的重写函数。 FightersmyFighters 完全一样,除了 Fighters 使用调用 Bar()FoomyFighters 使用 Foo调用 myBar()。有没有人有解决这两个问题的建议?或者我应该庆幸我找到了解决方案并继续我的生活...

在 class 上定义的所有方法都将实例作为第一个参数。惯例是调用实例 self 并这样引用它:

class Foo(object):
    def __init__(self):
        self.x = 1

其次,如果您不需要覆盖 parent class 的方法,只需将其省略即可:

class Fighters(Foo):
    # __init__ would be here, but I leave it out to inherit the method

    def func_1(self):
        ...

最后,如果您想引用 parent 的行为,请使用 super():

class MyFighters(Fighters):
    def __init__(self):
         super(MyFighters, self).__init__()
         self.y = 2

另见 this example and the official Python docs on classes

经过一些代码清理以创建一个 运行可用示例:

class Foo( object ):
        def __init__(self):
                print("foo init")
                self.foo_func()
        def foo_func(self):
                print("making bar")
                self.x = Bar()
        def foo_bar_func(self):
                self.x.bar_func()

class Fighters( Foo ):
        def __init__(self):
                print("fighters init")
                Foo.__init__(self)
        def func1(self):
                Foo.some_attribute
        def func2(self):
                Foo.someother_func()

class Bar(object):
        def __init__(self):
                print("bar init")
        def bar_func(self):
                #some stuff here
                print("bar bar_func")

实例化原始战士:

f1 = Fighters()
f1.foo_bar_func()

并从原始柱中调用方法 class:

fighters init
foo init
making bar
bar init
bar bar_func

现在我们可以用 MyFighters 子class Fighters,用 MyBar 子 Bar。使用对 MyBar 的构造函数调用覆盖 Foo class 的 foo_func is MyFighters,它可以执行您想要的操作而无需复制方法:

class MyFighters(Fighters):
        def foo_func(self):
                print("making mybar")
                self.x = MyBar()

class MyBar(Bar):
        def bar_func(self):
                print("mybar bar_func")

当我们创建一个新的战士时:

f2 = MyFighters()
f2.foo_bar_func()

我们看到它调用了新柱的方法 class:

fighters init
foo init
making mybar
bar init
mybar bar_func

显然,这只有效,因为创建 Bar 对象的方法对于 subclass 和替换来说是微不足道的。通常情况并非如此,因为它可能直接在 init 中完成,而不是调用方法来完成。因此,原始 classes 的设计使此示例成为 运行.