Python中的方法覆盖
先决条件: 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
具有多级和多级继承的方法覆盖
- 多重继承:当一个类派生自多个基类时,称为多重继承。
示例:让我们考虑一个示例,我们只想覆盖一个父类的方法。下面是实现。
# 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
名.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