Python 简明教程
Python - Method Overriding
Method Overriding in Python
Python method overriding 指在子类中定义一个与超类中同名方法一样的方法。在这种情况下,Python 解释器根据实际引用的对象决定在运行时调用哪个方法。
The Python method overriding refers to defining a method in a subclass with the same name as a method in its superclass. In this case, the Python interpreter determines which method to call at runtime based on the actual object being referred to.
你总是可以覆盖父类方法。覆盖父类方法的一个原因是,你可能希望在你的子类中实现特殊或不同的功能。
You can always override your parent class methods. One reason for overriding parent’s methods is that you may want special or different functionality in your subclass.
Example
在以下代码中,我们重写了 Parent 类的 myMethod 名方法。
In the code below, we are overriding a method named myMethod of Parent class.
# define parent class
class Parent:
def myMethod(self):
print ('Calling parent method')
# define child class
class Child(Parent):
def myMethod(self):
print ('Calling child method')
# instance of child
c = Child()
# child calls overridden method
c.myMethod()
当执行以上代码时,它会产生以下 output -
When the above code is executed, it produces the following output −
Calling child method
为了理解 Python 中的方法重写,让我们看另一个示例。我们将以下 Employee 类用作父类−
To understand Method Overriding in Python, let us take another example. We use following Employee class as parent class −
class Employee:
def __init__(self,nm, sal):
self.name=nm
self.salary=sal
def getName(self):
return self.name
def getSalary(self):
return self.salary
接下来,我们定义一个 SalesOfficer 类,它使用 Employee 作为父类。它继承父类的 instance 变量 name 和 salary。此外,子类还有另一个 instance 变量 incentive。
Next, we define a SalesOfficer class that uses Employee as parent class. It inherits the instance variables name and salary from the parent. Additionally, the child class has one more instance variable incentive.
我们将使用内置函数 super() ,它返回父类的引用,并在子构造函数 init () 方法中调用父构造函数。
We shall use built-in function super() that returns reference of the parent class and call the parent constructor within the child constructor init() method.
class SalesOfficer(Employee):
def __init__(self,nm, sal, inc):
super().__init__(nm,sal)
self.incnt=inc
def getSalary(self):
return self.salary+self.incnt
覆盖 getSalary() 方法可以向薪水中添加奖励。
The getSalary() method is overridden to add the incentive to salary.
Example
声明父类和子类的对象,并查看覆盖效果。完整的代码如下 −
Declare the object of parent and child classes and see the effect of overriding. Complete code is below −
class Employee:
def __init__(self,nm, sal):
self.name=nm
self.salary=sal
def getName(self):
return self.name
def getSalary(self):
return self.salary
class SalesOfficer(Employee):
def __init__(self,nm, sal, inc):
super().__init__(nm,sal)
self.incnt=inc
def getSalary(self):
return self.salary+self.incnt
e1=Employee("Rajesh", 9000)
print ("Total salary for {} is Rs {}".format(e1.getName(),e1.getSalary()))
s1=SalesOfficer('Kiran', 10000, 1000)
print ("Total salary for {} is Rs {}".format(s1.getName(),s1.getSalary()))
执行此代码时,它将生成以下 output -
When you execute this code, it will produce the following output −
Total salary for Rajesh is Rs 9000
Total salary for Kiran is Rs 11000