logo

Preglasitev metode v Pythonu

Predpogoj: Dedovanje v Pythonu

Preglasitev metode je zmožnost katerega koli objektno usmerjenega programskega jezika, ki omogoča podrazredu ali podrejenemu razredu, da zagotovi specifično izvedbo metode, ki jo že zagotavlja eden od njegovih nadrazredov ali nadrejenih razredov. Če ima metoda v podrazredu isto ime, enake parametre ali podpis in isti vrnjeni tip (ali podtip) kot metoda v svojem nadrazredu, se za metodo v podrazredu reče, da preglasiti metoda v super-razredu.



preglasitev-v-pythonu

Različico metode, ki se izvaja, bo določil objekt, ki se uporablja za njen priklic. Če se za priklic metode uporabi objekt nadrejenega razreda, se izvede različica v nadrejenem razredu, če pa se za priklic metode uporabi objekt podrazreda, se izvede različica v podrejenem razredu. Z drugimi besedami, tip predmeta, na katerega se nanaša (ne tip referenčne spremenljivke), je tisti, ki določa, katera različica preglasene metode bo izvedena.

primer:








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

>

>

Izhod:

 Inside Parent Inside Child>

Preglasitev metode z večkratnim in večnivojskim dedovanjem

  1. Večkratno dedovanje: Ko je razred izpeljan iz več kot enega osnovnega razreda, se imenuje večkratno dedovanje.

    primer: Oglejmo si primer, kjer želimo preglasiti metodo samo enega nadrejenega razreda. Spodaj je izvedba.

    datum na niz




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

    >

    >

    Izhod:

     Inside Child Inside Parent2>
    Večnivojsko dedovanje: Ko imamo razmerje med otrokom in vnukom.

    primer: Oglejmo si primer, kjer želimo preglasiti samo eno metodo enega od njenih nadrejenih razredov. Spodaj je izvedba.




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

    >

    >

    Izhod:

     Inside GrandChild Inside Parent>

Klicanje starševske metode znotraj preglasene metode

Metode nadrejenega razreda lahko kličete tudi znotraj preglasenih metod. To je na splošno mogoče doseči na dva načina.

    Uporaba Classname: Nadrejene metode razreda lahko pokličete z uporabo Parentclassname.method>znotraj preglasene metode.

    primer:




    kako preveriti velikost zaslona monitorja

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

    >

    >

    Izhod:

     Inside Parent Inside Child>
    Uporaba Super(): Pythonsuper()>funkcija nam omogoča eksplicitno sklicevanje na nadrejeni razred. V bistvu je uporaben tam, kjer moramo klicati funkcije nadrazreda. Vrne posredniški objekt, ki nam omogoča, da nadrejeni razred navedemo s 'super'.

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

    >

    >

    Izhod:

     Inside Parent Inside Child>

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

    pisanje json v datoteko python
    >

    >

    Izhod:

     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>