전제 조건: 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>
다중 및 다단계 상속으로 메서드 재정의
- 다중 상속: 클래스가 둘 이상의 기본 클래스에서 파생되는 경우 이를 다중 상속이라고 합니다.
예: 한 부모 클래스의 메서드만 재정의하려는 예를 생각해 보겠습니다. 아래는 구현입니다.
영화 123 ~
# 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>
다단계 상속: 자녀와 손자 관계가 있는 경우.예: 상위 클래스 중 하나의 메서드 하나만 재정의하려는 예를 생각해 보겠습니다. 아래는 구현입니다.
# 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>
재정의된 메서드 내에서 부모 메서드 호출
부모 클래스 메서드는 재정의된 메서드 내에서 호출될 수도 있습니다. 이는 일반적으로 두 가지 방법으로 달성할 수 있습니다.
- 클래스 이름 사용: 부모 클래스 메서드를 사용하여 부모 클래스 메서드를 호출할 수 있습니다.
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() 사용: Python
super()>함수는 부모 클래스를 명시적으로 참조할 수 있는 기능을 제공합니다. 기본적으로 슈퍼클래스 함수를 호출해야 하는 경우에 유용합니다. 이는 'super'로 상위 클래스를 참조할 수 있게 해주는 프록시 객체를 반환합니다.예시 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()> |
>
>
산출:
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>