2016-07-18 4 views
-1

ちょうどPythonを学んで、怒らないでください。子クラスで親メソッドを使用する| python

は、次のコードを考えてみましょう:私はそれを継承する場合、私は、子供の方法で親のprint_hello()を呼び出すことはできませんなぜ

class Parent: 
    def __init__(self): 
     pass 

    @staticmethod 
    def print_hello(): 
     print "hello" 

class Child(Parent): 
    def __init__(self): 
     super(Child, self).__init__()  

    @staticmethod 
    def print_all(): 
     print_hello() 
     print "world" 

質問はありますか?

+1

メソッドを呼び出す場合は、そのメソッドを呼び出すオブジェクトを明示的に指定する必要があります。 'print_hello()'だけでなく 'something.print_hello()'でなければなりません。今、「何か」が何であるべきかを理解する。 – user2357112

+1

また、Python 2を使用している場合、 'Parent'は他のものから継承しない場合、' object'から明示的に継承する必要があります。 – user2357112

+0

'Child.print_hello()'が動作します。ありがとうございました。 –

答えて

0

Child.print_hello()作品です。

質問が閉じます。

関連する問題