Go to Python syntax to learn object-oriented inheritance

Source: Internet
Author: User

Portal

Python syntax learning object-oriented inheritance

As long as it involves object-oriented, "class" is a synonym that must appear.

Classes and objects are the two main aspects of object-oriented programming. class creates a new type, and the object is an instance of this class.

Some concepts of the class:

Including the initialization method __init__, can be understood as constructs, self, understood as this, and so on in the previous article introduced, now learn the inheritance of classes.

Inherited:

One of the main benefits of object-oriented programming is the reuse of code, and one way to implement this reuse is through inheritance mechanisms. Inheritance can be fully understood as a type and subtype relationship between classes.

What to note: Inherit the syntax class derived class name ( base class name )://... The base class name is written in parentheses, and the base class is specified in the tuple at the time the class is defined. This is different from C #.

When to use inheritance: if I need to define a few classes, and there are some common properties and methods between classes and classes, then I can use the same properties and methods as members of the base class, and special methods and properties are defined in this class, so that only inherit the base class this action, you can access the properties and methods of the base class, It improves the extensibility of the code.

There are pros and cons to everything: one weakness of inheritance is that there may be special classes and other special places, and a class can be defined, and the class may be defined under it, which will cause the inherited line to grow longer, and with inheritance, any small change needs to redefine a class, which can easily cause explosive growth of the class. , producing a whole bunch of sub-categories that are slightly different. So there is a "multi-use combination less inheritance" principle, (I think the use of the two is the best bar *^◎^*)

Some of the characteristics of inheritance in Python:

1: The construction of the base class in inheritance (The __init__ () method) is not automatically called, it needs to be called specifically in the construction of its derived class. different from C #

2: When calling a method of the base class, you need to prefix the class name of the base class with the Self argument variable. Unlike calling a normal function in a class, you do not need to take the self argument

3:python always looks for a method of the corresponding type first, and if it cannot find the corresponding method in the derived class, it begins to look in the base class one by one. (Find the method that was called in this class before you can find it in the base class).

If more than one class is listed in an inheritance tuple, it is called "Multiple inheritance."

Example: Define a module in which a base class is defined:

Sub-class:

Output:

If you do not use the __init__ () initialization function in a subclass, the subclass inherits the properties of the base class, such as:

Output:

From: Joan Source: http://www.cnblogs.com/Joans/The author and the blog Garden Share, Welcome to reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

Go to Python syntax to learn object-oriented inheritance

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.