<p><a href="https://stackoverflow.com/questions/38778158/how-to-do-nested-class-and-inherit-inside-the-class?rq=1">Here</a>他们是这样做的</p>
<pre><code>super(MainClass.InnerSubClass, self).__init__(thing, otherThing)
</code></pre>
<p>因此,您可以在这里测试它,这是完整的工作示例</p>
<pre><code>class SomeClass(object):
def __init__(self):
"""lots of other working stuff"""
class MainClass(object):
def __init__(self):
self.stuff = []
self.moreStuffs = []
class InnerClass(object):
def __init__(self, thing, otherThing):
self.thing = thing
self.otherThing = otherThing
self.otherStuff = []
class InnerSubClass(InnerClass):
def __init__(self, thing, otherThing, newThing):
super(MainClass.InnerSubClass, self).__init__(thing, otherThing)
self.newThing = newThing
"""other code that worked before the addition of 'InnerSubClass'"""
def doSomething(self):
innerclass = self.InnerSubClass('thisthing', 'thatthing', 'thingthing')
print("just more thing words %s" % innerclass.newThing)
print("and I also inherit from InnerClass %s" % innerclass.otherThing)
myThing = MainClass()
myThing.doSomething()
</code></pre>
<p>输出为</p>
<pre><code>just more thing words thingthing
and I also inherit from InnerClass thatthing
</code></pre>