-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"
質問はありますか?
メソッドを呼び出す場合は、そのメソッドを呼び出すオブジェクトを明示的に指定する必要があります。 'print_hello()'だけでなく 'something.print_hello()'でなければなりません。今、「何か」が何であるべきかを理解する。 – user2357112
また、Python 2を使用している場合、 'Parent'は他のものから継承しない場合、' object'から明示的に継承する必要があります。 – user2357112
'Child.print_hello()'が動作します。ありがとうございました。 –