2015-08-14 96 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() 

由於繼承對父類創建依賴關係,所以在處理多重繼承時已經開始使用組合了。