Inheritance of classes in Python design pattern 1:python

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.