In object-oriented program design, inheritance (inheritance) allows subclasses to derive properties and methods from the parent class, while subclasses can add or reload any method in their parent class. In the object model of C + + and Java, the constructor of the subclass automatically calls the constructor of the parent class, but not in Python, you must display the constructor that invokes the parent class in the subclass.
In [1]:classEmployee: ...:def __init__(self,name,salary=0): ...: Self.name=name ...: self.salary=salary ...: in [2]:classDesigner (Employee):def __init__(self,name): ...: Self.name=name ...: in [3]: D = Designer ("Alex") in [4]: d.salary---------------------------------------------------------------------------Attributeerror Traceback (most recent)<ipython-input-4-c8934453b0a8>inch<module>()----> 1D.salaryattributeerror:'Designer'object has no attribute'Salary'In [5]: E = Employee ('Alex') in [6]: e.salaryout[6]: 0
It can be seen that the subclass designer does not inherit the initialization function of the parent class, and if inheritance is required, add: employee.__init__ (self, name, 5000).
Inheritance of classes in Python design pattern 1:python