Python 中的方法重写

Python 中的方法重写是指创建两个同名但编程逻辑不同的方法。方法重写的概念允许我们在子类中更改或重写父类函数。

要在 Python 编程语言中重写方法或执行方法重写,您必须满足某些条件,它们是:

  • 您不能在同一个类中重写方法。这意味着您必须使用继承概念在子类中进行重写。
  • 要重写父类方法,您必须在子类中创建一个同名且参数数量相同的方法。

例如,如果您有一个计算组织所有员工薪资增长的函数。但是特定的部门或人员会获得不同的百分比。在这种情况下,您可以重写部门类中现有的方法并编写自己的逻辑。

在本节中,我们将通过一个示例讨论如何执行方法重写。

Python 方法重写示例

在此示例中,我们创建了一个 employee 类,其中包含一个打印消息的消息函数。接下来,我们创建了一个继承自 Employee 类的 department。在其中,我们创建了一个同名的 message 函数,并带有不同的打印消息。

这是 Python 方法重写的一个简单演示。在这里,我们只是重写了消息。接下来,我们为 Employee 和 Department 类创建了对象并调用了该消息函数。

 
class Employee:
      
    def message(self):
        print('This message is from Emp')
  
class Department(Employee):
  
    def message(self):
        print('This Department is inherited from Emp')
  
emp = Employee()
emp.message()
  
print('------------')
dept = Department()
dept.message()
This message is from Emp
------------
This Department is inherited from Emp

正如您所看到的,emp 对象打印来自 Employee 消息函数的字符串。而 dept.message() 则打印来自 Department 的测试。

在此方法重写示例中,我们创建了另一个继承自 Employee 的类。接下来,我们还将重写 Sales 类中的 message 函数。请参考 示例。

class Employee:
      
    def message(self):
        print('This message is from Employee ')
  
class Department(Employee):
  
    def message(self):
        print('This Department inherited from Employee')
 

class Sales(Employee):
  
    def message(self):
        print('This Sales is also inherited from Employee')
         
emp = Employee()
emp.message()
  
print('------------')
dept = Department()
dept.message()
 

print('------------')
sl = Sales()
sl.message()
This message is from Employee 
------------
This Department inherited from Employee
------------
This Sales is also inherited from Employee

Python 多重继承中的方法重写

这个 Python 示例展示了如何在多重继承情况下执行方法重写。

请参考 Python 中的 继承

class Employee:
      
    def message(self):
        print('This message is from Employee')
  
class Department(Employee):
  
    def message(self):
        print('This Department is inherited from Employee')
 
class Sales(Department):
  
    def message(self):
        print('This Sales is inherited from Employee')
         
emp = Employee()
emp.message()
  
print('------------')
dept = Department()
dept.message()
 
print('------------')
sl = Sales()
sl.message()

多重继承方法重写输出

This message is from Employee 
------------
This Department is inherited from Employee
------------
This Sales is inherited from Employee

带参数

到目前为止,我们只更改了打印语句。我的意思是,重写了没有参数的函数。在此示例中,我们在父类中创建了一个带有两个参数的 add 函数,在子类中创建了一个带有三个参数的 add 函数。

class Employee:
      
    def add(self, a, b):
        print('The Sum of Two = ', a + b)
  
class Department(Employee):
  
    def add(self, a, b, c):
        print('The Sum of Three = ', a + b + c)
         
emp = Employee()
emp.add(10, 20)
  
print('------------')
dept = Department()
dept.add(50, 130, 90)
Method Overriding in Python Example

在方法重写中调用父函数

在此示例中,我们正在从重写的方法中调用父函数。

class Employee:
      
    def message(self):
        print('This message is from Employee')
  
class Department(Employee):
  
    def message(self):
        Employee.message(self)
        print('This Department is inherited from Employee')
  
emp = Employee()
emp.message()
  
print('------------')
dept = Department()
dept.message()
This message is from Employee
------------
This message is from Employee
This Department is inherited from Employee

从上面的截图可以看出,两者根据对象都给出了不同的结果。这里,Employee.message(self) 调用了 print(‘This message is from Employee’) 语句,并在该 print 函数 中打印了消息。您可以使用 super() 函数来调用父方法,而不是使用类名。

class Employee:
      
    def message(self):
        print('This message is from Employee')
  
class Department(Employee):
  
    def message(self):
        super().message()
        print('This Department is inherited from Employee')
  
emp = Employee()
emp.message()
  
print('------------')
dept = Department()
dept.message()
This message is from Employee 
------------
This message is from Employee 
This Department is inherited from Employee