logo

Metodes ignorēšana Python

Priekšnosacījums: Mantojums Python valodā

Metodes ignorēšana ir jebkuras objektorientētas programmēšanas valodas spēja, kas ļauj apakšklasei vai pakārtotajai klasei nodrošināt konkrētu metodes ieviešanu, ko jau nodrošina kāda no tās virsklasēm vai vecākklasēm. Ja metodei apakšklasē ir tāds pats nosaukums, tie paši parametri vai paraksts un tāds pats atgriešanas veids (vai apakštips) kā metodei tās virsklasē, tad apakšklasē esošajai metodei ir tāds pats nosaukums. ignorēt metode superklasē.



overriding-in-python

Izpildītās metodes versiju noteiks objekts, kas tiek izmantots tās izsaukšanai. Ja metodes izsaukšanai tiek izmantots vecākklases objekts, tad tiks izpildīta vecākklasē esošā versija, bet, ja metodes izsaukšanai tiek izmantots apakšklases objekts, tad tiks izpildīta versija bērnklasē. Citiem vārdiem sakot, tas ir objekta tips, uz kuru attiecas atsauce (nevis atsauces mainīgā tips), kas nosaka, kura ignorētās metodes versija tiks izpildīta.

Piemērs:








# 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()>

>

>

Izvade:

 Inside Parent Inside Child>

Metodes ignorēšana ar vairāku un daudzlīmeņu pārmantošanu

  1. Daudzkārtēja mantošana: Ja klase ir atvasināta no vairāk nekā vienas bāzes klases, to sauc par vairāku mantojumu.

    Piemērs: Apskatīsim piemēru, kur mēs vēlamies ignorēt tikai vienas vecākklases metodi. Zemāk ir tā ieviešana.

    alya manasa




    # 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()>

    >

    >

    Izvade:

     Inside Child Inside Parent2>
    Daudzlīmeņu mantošana: kad mums ir bērna un mazbērna attiecības.

    Piemērs: Apskatīsim piemēru, kurā mēs vēlamies ignorēt tikai vienu vienas no tās vecākklases metodi. Zemāk ir tā ieviešana.




    # 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()>

    >

    >

    Izvade:

     Inside GrandChild Inside Parent>

Vecāku metodes izsaukšana ignorētajā metodē

Ignorēto metožu ietvaros var izsaukt arī vecākklases metodes. To parasti var panākt divos veidos.

    Klases nosaukuma izmantošana: vecāku klases metodes var izsaukt, izmantojot vecākuclassname.method>ignorētās metodes iekšpusē.

    Piemērs:




    Instagram priekšrocības personīgai lietošanai

    # 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()>

    >

    >

    Izvade:

     Inside Parent Inside Child>
    Izmantojot Super(): Pythonsuper()>funkcija nodrošina mums iespēju skaidri atsaukties uz vecāku klasi. Būtībā tas ir noderīgi, ja mums ir jāizsauc superklases funkcijas. Tas atgriež starpniekservera objektu, kas ļauj mums atsaukties uz vecāku klasi ar “super”.

    1. piemērs:




    mantošanas programma python

    # 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()>

    >

    >

    Izvade:

     Inside Parent Inside Child>

    2. piemērs:




    # 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>)>

    >

    >

    Izvade:

     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>