Python:调用父类方法
类是用户定义的蓝图或原型,从中创建对象。类提供了一种将数据和功能捆绑在一起的方法。创建一个新类会创建一种新类型的对象,允许创建该类型的新实例。
例子:
# Python program to demonstrate
# classes
class cls:
# Constructor
def __init__(self, fname, mname, lname):
self.firstname = fname
self.middlename = mname
self.lastname = lname
# class Function
def print(self):
print(self.firstname, self.middlename, self.lastname)
# Use the Parent class to create an object
# and then execute the print method:
x = cls("Geeks", "for", "Geeks")
x.print()
输出:
Geeks for Geeks
调用父类方法
要了解父类的概念,您必须了解Python中的继承。简单来说,继承是一个类(通常称为子类或子类)从另一个类(通常称为父类或超类)继承属性的概念。
但是你有没有想过在子类的帮助下调用父类中定义的函数?这可以使用Python来完成。您只需要创建子类的对象并使用dot(.)
运算符调用父类的函数。
例子:
# Python code to demonstrate
# parent call method
class Parent:
# create a parent class method
def show(self):
print("Inside Parent class")
# create a child class
class Child(Parent):
# Create a child class method
def display(self):
print("Inside Child class")
# Driver's code
obj = Child()
obj.display()
# Calling Parent class
obj.show()
输出
Inside Child class
Inside Parent class
方法覆盖后调用父类方法
方法覆盖是任何面向对象编程语言的一种能力,它允许子类或子类提供已由其超类或父类之一提供的方法的特定实现。
父类方法也可以在被覆盖的方法中调用。这通常可以通过两种方式来实现。
- 使用类名:父类的方法可以通过在被覆盖的方法中使用父
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