如何使用父类对象调用子类方法

问题描述 投票:0回答:2

我对Python继承有点新意。我有一个像这样的代码

class Parent(object):
'''
Class for Parent
'''

def __init__(self, host, user, password):
    self.host = host
    self.user = user
    self.password = password
    self.child = Child()

def method1(self,param1, param2, param3):
    self.child.method1(param1, param2, param3)

以下是儿童班。父类和子类都在同一个文件中

class Child(Parent):
'''
Class for Child
'''
  def __init__(self):
    super(Child).__init__()

  def method1(self, param1, param2, param3):
    // My actual code will be here

现在,我有另一个python脚本,我将初始化父对象并调用method1

 client = Parent(host, user, password)
 client.method1(param1, param2, param3)

通过上面的方法,我期待Child的method1将被执行。但事实并非如此。我收到以下错误

 TypeError: super() takes at most 1 argument (0 given)

这是什么解决方案..?我哪里做错了..?

python python-2.7
2个回答
1
投票

在Python 2中,super有两个参数,正如错误消息所解释的那样。你这样使用它:

super(Child, self).__init__()

但是,您还必须将所有必需的参数传递给__init__。由于你的Parent需要一个hostuserpassword,你需要这些super初始化器。所以你可能想要在Child中这样的东西:

def __init__(self, host, user, password):
    super(Child, self).__init__(host, user, password)

虽然由于Child.__init__除了尝试super之外实际上没有做任何事情,更简单的事情是首先不要覆盖__init__

无论如何,一旦你解决了super问题,无论哪种方式,你只会有一个无限递归 - Parent创建一个Child(),它超越Parent构造函数,因此创建另一个Child(),依此类推,直到你得到一个例外。

实际上,你的设计非常奇怪。对象需要保持对其子类之一的实例的引用(更不用说创建)是罕见的。通常,您只想将继承用作继承。像这样的东西:

class Parent(object):
    '''
    Class for Parent
    '''

    def __init__(self, host, user, password):
        self.host = host
        self.user = user
        self.password = password
        self.child = Child()

class Child(Parent):
    def method1(self, param1, param2, param3):
        # actual code here

现在,如果你创建一个child = Child(host, user, password),你可以在上面调用child.method1(arg1, arg2, arg3)。如果您想在Parent中提供默认实现,则可以。您甚至可以在Child和其他任何孩子在他们自己的代码之前或之后通过super调用之前或之后实现它。对于这个玩具模型,你真正想要的是哪一个是不可能的,但对于真正的课程,这通常很明显。


0
投票

我设法在Singleton类的帮助下实现了这一目标。这就是我所做的。这是父类。

class Parent(object):
'''
Class for Parent
'''

def __init__(self, host, user, password):
    self.host = host
    self.user = user
    self.password = password

def helper_method1():
    // Implementation

然后我创建了一个单例类

class ParentObjMgr(object):
    '''
    A Singleton that stores the objects of different child classes.
    The child class needs to be instantiated here.
    This class needs to be instantiated in the caller.
    '''
    __metaclass__ = Singleton

    def __init__(self, host, user, password):
        self._host = host
        self._user = user
        self._password = password
        self._child1 = None
        self._child2 = None

    def get_child1(self):
        if not self._child1:
            self._child1 = Child1(self._host, self._user, self._password)
        return self._child1

    def get_child2(self):
        if not self._child2:
            self._child2 = Child2(self._host, self._user, self._password)
        return self._child2

现在我将实现子类

class Child1(Parent):
    // Implementation

class Child2(Parent):
    // Implementation
    self.helper_method1()

现在在调用者中,我会这样做

parent_object_mgr=ParentObjMgr(self.host, self.user, self.password)                  
child1=parent_object_mgr.get_child1()
child2=parent_object_mgr.get_child2()

这就是我在单个类对象的帮助下设法访问所有子类对象的方法。现在,我不必实例化子类,无论它们被调用到哪里,这都是我正在寻找的。请告诉我这是否是一个有效的解决方案(或)我需要做些什么来改进它。谢谢

© www.soinside.com 2019 - 2024. All rights reserved.