2015-08-14 95 views
-2

我在这是由父母和子女继承GParent类TestMethod的..调用父方法在Python

我怎么能这样做?

我试过,但它不工作...

GParent.testmethod(self) 

class GParent(): 
    def testmethod(self): 
     print "This is test method" 


class Parent(): 
    def testmethod(self): 
     print "This is test method" 


class Child(Parent): 
    def __init__(self): 
     print "This is init method" 
     GParent.testmethod(self) 

c = Child() 
+3

你的'Parent'和'Child'不是从'GParent'继承的。你打算这么做吗? – BrenBarn

+0

可能重复[在Python中从子类调用父类的方法?](http://stackoverflow.com/questions/805066/call-a-parent-classs-method-from-child-class-in-python) –

回答

1

首先:https://docs.python.org/2/tutorial/classes.html#inheritance

无论如何...

GParent.testmethod(self) <-- calling a method before it is defined 

class GParent(): <-- always inherit object on your base class to ensure you are using new style classes 
    def testmethod(self): 
     print "This is test method" 


class Parent(): <-- not inheriting anything 
    def testmethod(self): <-- if you were inheriting GParent you would be overriding the method that is defined in GParent here. 
     print "This is test method" 


class Child(Parent): 
    def __init__(self): 
     print "This is init method" 
     GParent.testmethod(self) <-- if you want to call the method you are inheriting you would use self.testmethod() 

c = Child() 

看看这个代码,并运行它,也许它会帮助你。

from __future__ import print_function #so we can use python 3 print function 

class GParent(object): 
    def gparent_testmethod(self): 
     print("Grandparent test method ") 


class Parent(GParent): 
    def parent_testmethod(self): # 
     print("Parent test method") 


class Child(Parent): 
    def child_testmethod(self): 
     print("This is the child test method") 

c = Child() 
c.gparent_testmethod() 
c.parent_testmethod() 
c.child_testmethod() 
0

不能调用GParent的testmethodGParent实例作为其第一个参数。

继承

class GParent(object): 
    def testmethod(self): 
     print "I'm a grandpa" 

class Parent(GParent): 

    # implicitly inherit __init__()                             

    # inherit and override testmethod()                            
    def testmethod(self): 
     print "I'm a papa" 

class Child(Parent): 

    def __init__(self): 
     super(Child, self).__init__() 
     # You can only call testmethod with an instance of Child 
     # though technically it is calling the parent's up the chain                      
     self.testmethod() 

    # inherit parent's testmethod implicitly 

c = Child() # print "I'm a papa" 

但是,调用父母的方法明确地两种方式是通过组合物或类方法

组成

class Parent(object): 
    def testmethod(self): 
     print "I'm a papa" 

class Child(object): 
    def __init__(self): 
     self.parent = Parent() 

     # call own's testmethod                              
     self.testmethod() 

     # call parent's method                              
     self.parentmethod() 

    def parentmethod(self): 
     self.parent.testmethod() 

    def testmethod(self): 
     print "I'm a son" 

c = Child() 

类方法

class Parent(object): 
    @classmethod 
    def testmethod(cls): 
     print "I'm a papa" 

class Child(object): 
    def __init__(self): 
     # call own's testmethod                              
     self.testmethod() 

     # call parent's method                              
     Parent.testmethod() 

    def testmethod(self): 
     print "I'm a son" 

c = Child() 

由于继承对父类创建依赖关系,所以在处理多重继承时已经开始使用组合了。