Warunek wstępny: Dziedziczenie w Pythonie
Nadpisywanie metod to zdolność dowolnego obiektowego języka programowania, która pozwala podklasie lub klasie podrzędnej zapewnić specyficzną implementację metody, która jest już zapewniona przez jedną z jej nadklas lub klas nadrzędnych. Kiedy metoda w podklasie ma tę samą nazwę, te same parametry lub sygnaturę i ten sam typ zwracany (lub podtyp) co metoda w jej nadklasie, wówczas mówi się, że metoda w podklasie nadpisanie metoda w nadklasie.

Wersja metody, która zostanie wykonana, zostanie określona przez obiekt użyty do jej wywołania. Jeśli do wywołania metody zostanie użyty obiekt klasy nadrzędnej, to zostanie wykonana wersja z klasy nadrzędnej, natomiast jeśli do wywołania metody zostanie użyty obiekt klasy nadrzędnej, to zostanie wykonana wersja z klasy potomnej. Innymi słowy, to typ obiektu, do którego się odwołujemy (a nie typ zmiennej referencyjnej), określa, która wersja przesłoniętej metody zostanie wykonana.
Przykład:
# 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()> |
>
>
Wyjście:
Inside Parent Inside Child>
Nadpisywanie metod przy dziedziczeniu wielokrotnym i wielopoziomowym
- Dziedziczenie wielokrotne: Kiedy klasa wywodzi się z więcej niż jednej klasy bazowej, nazywa się to dziedziczeniem wielokrotnym.
Przykład: Rozważmy przykład, w którym chcemy zastąpić metodę tylko jednej klasy nadrzędnej. Poniżej realizacja.
typ w Javie
# 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()>>
>
Wyjście:
Inside Child Inside Parent2>
Dziedziczenie wielopoziomowe: Kiedy mamy relację dziecko i wnuk.Przykład: Rozważmy przykład, w którym chcemy zastąpić tylko jedną metodę jednej z jej klas nadrzędnych. Poniżej realizacja.
# 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()>>
>
Wyjście:
Inside GrandChild Inside Parent>
Wywołanie metody Parent w ramach metody nadpisanej
Metody klas nadrzędnych można również wywoływać w ramach metod przesłoniętych. Można to ogólnie osiągnąć na dwa sposoby.
- Używanie nazwy klasy: Metody klasy rodzica można wywoływać za pomocą klasy Parent
classname.method>wewnątrz nadpisanej metody.Przykład:
zwracanie tablic w Javie
# 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()> |
>
>
Wyjście:
Inside Parent Inside Child>Używanie Super(): Python
super()>Funkcja zapewnia nam możliwość bezpośredniego odwoływania się do klasy nadrzędnej. Zasadniczo jest to przydatne, gdy musimy wywołać funkcje nadklasy. Zwraca obiekt proxy, który pozwala nam odwołać się do klasy nadrzędnej poprzez „super”.Przykład 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()> |
>
>
Wyjście:
Inside Parent Inside Child>
Przykład 2:
jak pobierać muzykę
# 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>)> |
>
>
Wyjście:
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>