object inheritance and nested cmd

后端 未结 3 1395
深忆病人
深忆病人 2021-01-07 01:11

This is probably a basic OO question: I\'m trying to do a nested console menu with cmd which has gone well. I also want all my sub-consoles to have access to the same object

3条回答
  •  庸人自扰
    2021-01-07 01:26

    You don't need multiple inheritance, but you need to give obj1 and obj2 to the inherited objects, except if you give some default values to obj1 and obj2.

    class SubConsole1(MainConsole):
        def __init__(self, obb1, obj2):
            MainConsole.__init__(self, obj1, obj2)
            self.prompt = "1>"
        def do_action(self,args):
            print self.obj1.someattr1 # Doesn't work
    

    instanciated by :

    sub1 = SubConsole1(object1, object2)
    

提交回复
热议问题