logo

Python でのメソッドのオーバーライド

前提条件: Python の継承

メソッドのオーバーライドは、サブクラスまたは子クラスが、そのスーパークラスまたは親クラスの 1 つによってすでに提供されているメソッドの特定の実装を提供できるようにするオブジェクト指向プログラミング言語の機能です。サブクラス内のメソッドがそのスーパークラス内のメソッドと同じ名前、同じパラメータまたはシグネチャ、および同じ戻り値の型 (またはサブタイプ) を持つ場合、サブクラス内のメソッドは次のようになります。 オーバーライド スーパークラスのメソッド。



Pythonでのオーバーライド

実行されるメソッドのバージョンは、そのメソッドの呼び出しに使用されるオブジェクトによって決まります。親クラスのオブジェクトを使用してメソッドを呼び出す場合は、親クラスのバージョンが実行されますが、サブクラスのオブジェクトを使用してメソッドを呼び出す場合は、子クラスのバージョンが実行されます。言い換えれば、オーバーライドされたメソッドのどのバージョンが実行されるかを決定するのは、参照されるオブジェクトの型 (参照変数の型ではない) です。

例:








# Python program to demonstrate> # method overriding> > > # Defining parent class> class> Parent():> > ># Constructor> >def> __init__(>self>):> >self>.value>=> 'Inside Parent'> > ># Parent's show method> >def> show(>self>):> >print>(>self>.value)> > # Defining child class> class> Child(Parent):> > ># Constructor> >def> __init__(>self>):> >self>.value>=> 'Inside Child'> > ># Child's show method> >def> show(>self>):> >print>(>self>.value)> > > # Driver's code> obj1>=> Parent()> obj2>=> Child()> > obj1.show()> obj2.show()>

>

>

出力:

 Inside Parent Inside Child>

複数およびマルチレベルの継承によるメソッドのオーバーライド

  1. 多重継承: クラスが複数の基本クラスから派生する場合、それは 多重継承 と呼ばれます。

    例: 1 つの親クラスのみのメソッドをオーバーライドする例を考えてみましょう。以下は実装です。

    10/50




    # Python program to demonstrate> # overriding in multiple inheritance> > > # Defining parent class 1> class> Parent1():> > ># Parent's show method> >def> show(>self>):> >print>(>'Inside Parent1'>)> > # Defining Parent class 2> class> Parent2():> > ># Parent's show method> >def> display(>self>):> >print>(>'Inside Parent2'>)> > > # Defining child class> class> Child(Parent1, Parent2):> > ># Child's show method> >def> show(>self>):> >print>(>'Inside Child'>)> > > # Driver's code> obj>=> Child()> > obj.show()> obj.display()>

    >

    >

    出力:

     Inside Child Inside Parent2>
    多レベルの継承: 子と孫の関係がある場合。

    例: いずれかの親クラスの 1 つのメソッドのみをオーバーライドする例を考えてみましょう。以下は実装です。




    # Python program to demonstrate> # overriding in multilevel inheritance> > > # Python program to demonstrate> # overriding in multilevel inheritance> > > class> Parent():> > ># Parent's show method> >def> display(>self>):> >print>(>'Inside Parent'>)> > > # Inherited or Sub class (Note Parent in bracket)> class> Child(Parent):> > ># Child's show method> >def> show(>self>):> >print>(>'Inside Child'>)> > # Inherited or Sub class (Note Child in bracket)> class> GrandChild(Child):> > ># Child's show method> >def> show(>self>):> >print>(>'Inside GrandChild'>)> > # Driver code> g>=> GrandChild()> g.show()> g.display()>

    >

    >

    出力:

     Inside GrandChild Inside Parent>

オーバーライドされたメソッド内での親のメソッドの呼び出し

親クラスのメソッドは、オーバーライドされたメソッド内で呼び出すこともできます。これは通常、2 つの方法で実現できます。

    クラス名の使用: 親のクラス メソッドは、親を使用して呼び出すことができます。classname.method>オーバーライドされたメソッド内。

    例:




    ジャワ島

    # Python program to demonstrate> # calling the parent's class method> # inside the overridden method> > > class> Parent():> > >def> show(>self>):> >print>(>'Inside Parent'>)> > class> Child(Parent):> > >def> show(>self>):> > ># Calling the parent's class> ># method> >Parent.show(>self>)> >print>(>'Inside Child'>)> > # Driver's code> obj>=> Child()> obj.show()>

    >

    >

    出力:

     Inside Parent Inside Child>
    Super() の使用: Pythonsuper()>この関数は、親クラスを明示的に参照する機能を提供します。これは基本的に、スーパークラス関数を呼び出す必要がある場合に便利です。これは、「スーパー」によって親クラスを参照できるようにするプロキシ オブジェクトを返します。

    例 1:




    # Python program to demonstrate> # calling the parent's class method> # inside the overridden method using> # super()> > > class> Parent():> > >def> show(>self>):> >print>(>'Inside Parent'>)> > class> Child(Parent):> > >def> show(>self>):> > ># Calling the parent's class> ># method> >super>().show()> >print>(>'Inside Child'>)> > # Driver's code> obj>=> Child()> obj.show()>

    >

    Javaのカスタマイズされた例外

    >

    出力:

     Inside Parent Inside Child>

    例 2:




    # Program to define the use of super()> # function in multiple inheritance> class> GFG1:> >def> __init__(>self>):> >print>(>'HEY !!!!!! GfG I am initialised(Class GEG1)'>)> > >def> sub_GFG(>self>, b):> >print>(>'Printing from class GFG1:'>, b)> > # class GFG2 inherits the GFG1> class> GFG2(GFG1):> >def> __init__(>self>):> >print>(>'HEY !!!!!! GfG I am initialised(Class GEG2)'>)> >super>().__init__()> > >def> sub_GFG(>self>, b):> >print>(>'Printing from class GFG2:'>, b)> >super>().sub_GFG(b>+> 1>)> > # class GFG3 inherits the GFG1 ang GFG2 both> class> GFG3(GFG2):> >def> __init__(>self>):> >print>(>'HEY !!!!!! GfG I am initialised(Class GEG3)'>)> >super>().__init__()> > >def> sub_GFG(>self>, b):> >print>(>'Printing from class GFG3:'>, b)> >super>().sub_GFG(b>+> 1>)> > > # main function> if> __name__>=>=> '__main__'>:> > ># created the object gfg> >gfg>=> GFG3()> > ># calling the function sub_GFG3() from class GHG3> ># which inherits both GFG1 and GFG2 classes> >gfg.sub_GFG(>10>)>

    >

    >

    出力:

     HEY !!!!!! GfG I am initialised(Class GEG3) HEY !!!!!! GfG I am initialised(Class GEG2) HEY !!!!!! GfG I am initialised(Class GEG1) Printing from class GFG3: 10 Printing from class GFG2: 11 Printing from class GFG1: 12>