2021年3月22日星期一

How to reuse code from parent class efficiently? (python)

Is there any nice way to reuse code from parent class other than just copy paste all code from parent class and replace some code if I want to change few lines of code are in the middle of the code?

  1. I can't change code in parent class directly.
  2. Most of codes are same but only few lines of code are different.
  3. Real code is hundreds lines of code and only few lines of code is different between parent and child class.

parent class:

class Parent():      def example(self):          doSomething1()          doSomething2()              ...          doSomething100()          doSomething101()              ...          doSomething200()          return  

child class:

class Child(Parent):      def example(self):          doSomething1()          doSomething2()              ...          doSomething100()          doSomethingNew() #add a new line          #doSomething101() #delete a line          doSomethingReplace() #replace a line              ...          doSomething200()          return  
https://stackoverflow.com/questions/66740400/how-to-reuse-code-from-parent-class-efficiently-python March 22, 2021 at 12:37PM

没有评论:

发表评论