Python syntax Learning Object-oriented Inheritance

Source: Internet
Author: User

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

Class and object are two main aspects of object-oriented programming.ClassCreate a new type, whileThe object isInstance of this class.

Class concepts:

Including the initialization method _ init __, which can be understood as constructor, self, and this.ArticleAs described in, we will now learn the inheritance of classes.

Inheritance:

One of the main benefits of object-oriented programming isCodeOfReuse, One of the ways to achieve this reuse is through the inheritance mechanism. Inheritance can be fully understood as the relationship between types and subtypes between classes.

Note:Inheritance syntaxClass derived class name (Base Class Name): //... In the brackets for writing the base class name, when the basic class is defined,The. This is different from C.

When to use inheritance: If I need to define several classes and there are some common attributes and methods between classes, then I can use the same attributes and methods as the base class members, special methods and attributes are defined in this class. In this way, you only need to inherit the base class action to access the attributes and methods of the base class, it improves code scalability.

Everything has advantages and disadvantages: one weakness of inheritance is that there may be other special points in this class, and a class will be defined, and classes may be further defined under it, in this way, the inherited line will become longer and longer,If inheritance is used, a new class needs to be defined for any small change, which can easily lead to explosive growth of classes and produce a large number of sub-classes with slight differences. so there is a "Multi-Purpose Combination with less inheritance" principle. (I think it is best to use both of them. * ^ ◎ ^ *)

Some features of inheritance in Python:

1: The Construction (_ init _ () method) of the base class in the inheritance class will not be automatically called. It needs to be called in the construction of its derived class. Different from C #

2: when calling a base class method, you must add the base class name prefix and the self parameter variable. Unlike calling a common function in a class, the self parameter is not required.

3: Python always first looks for the corresponding type of method. If it cannot find the corresponding method in the derived class, it starts to look for it one by one in the base class. (Search for the called method in this class before finding it in the base class ).

If more than one class is listed in the inheritance tuples, it is called "Multi-inheritance ".

Instance: defines a module, which defines a base class:

Subclass:

Output:

If the _ init _ () initialization function is not used in the subclass, The subclass inherits the attributes of the base class, for example:

Output:

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.