Prepísanie metódy v Pythone
Predpoklad: Dedičnosť v Pythone
Prepísanie metódy je schopnosť ľubovoľného objektovo orientovaného programovacieho jazyka, ktorá umožňuje podtriede alebo podradenej triede poskytovať špecifickú implementáciu metódy, ktorú už poskytuje jedna z jej nadtried alebo rodičovských tried. Ak má metóda v podtriede rovnaký názov, rovnaké parametre alebo podpis a rovnaký návratový typ (alebo podtyp) ako metóda vo svojej nadtriede, potom sa o metóde v podtriede hovorí, že prepísať metóda v nadtriede.
Verzia metódy, ktorá sa vykoná, bude určená objektom, ktorý sa použije na jej vyvolanie. Ak sa na vyvolanie metódy použije objekt nadradenej triedy, vykoná sa verzia v nadradenej triede, ale ak sa na vyvolanie metódy použije objekt podtriedy, vykoná sa verzia v podradenej triede. Inými slovami, je to typ objektu, na ktorý sa odkazuje (nie typ referenčnej premennej), ktorý určuje, ktorá verzia prepísanej metódy bude vykonaná.
Príklad:
# 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()> |
Výkon:
Inside Parent Inside Child
Prepísanie metódy s viacnásobnou a viacúrovňovou dedičnosťou
- Viacnásobné dedičstvo: Keď je trieda odvodená z viac ako jednej základnej triedy, nazýva sa viacnásobná dedičnosť.
Príklad: Uvažujme o príklade, kde chceme prepísať metódu iba jednej nadradenej triedy. Nižšie je uvedená implementácia.
# 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()>Výkon:
Inside Child Inside Parent2
Viacúrovňové dedičstvo: Keď máme vzťah medzi dieťaťom a vnukom.Príklad: Uvažujme o príklade, kde chceme prepísať iba jednu metódu jednej z jej rodičovských tried. Nižšie je uvedená implementácia.
# 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()>Výkon:
Inside GrandChild Inside Parent
Volanie metódy rodiča v rámci prepísanej metódy
V rámci prepísaných metód možno volať aj metódy rodičovskej triedy. Vo všeobecnosti sa to dá dosiahnuť dvoma spôsobmi.
- Použitie názvu triedy: Metódy rodičovskej triedy možno volať pomocou nadradenej triedy
classname.method> vnútri prepísanej metódy. Príklad:
# 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()> |
Výkon:
Inside Parent Inside ChildPoužitie Super(): Python
super()> funkcia nám poskytuje možnosť explicitne odkazovať na rodičovskú triedu. Je to v podstate užitočné tam, kde musíme volať funkcie nadtriedy. Vracia objekt proxy, ktorý nám umožňuje odkazovať na rodičovskú triedu pomocou „super“. Príklad 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()> |
Výkon:
Inside Parent Inside Child
Príklad 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> )> |
Výkon:
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