Python Object-oriented Inheritance code explanation, python object-oriented explanation

Source: Internet
Author: User

Python Object-oriented Inheritance code explanation, python object-oriented explanation

This article focuses on the Inheritance of Python object-oriented, as detailed below.

Python inheritance

That is, a derived class inherits the fields and methods of the baseline class. Inheritance also allows the object of a derived class to be treated as a base class object. For example, there is a design where a Cat-type object is derived from the Animal class, which simulates a "is-a" relationship (for example, Cat is an Animal ).

Inheritance implements code reuse.

Basic inherited Syntax:

Class derived class name (base class name 1 [, base class name 2...]):

The base class name is written in brackets. The basic class is specified in the tuples when the class is defined. If more than one class is listed in the inheritance tuples, it is called "Multi-inheritance ".

Multi-Inheritance

Python supports multiple inheritance, that is, a subclass can have multiple parent classes separated by commas.

In multi-inheritance, there are so many child classes. How do I call the constructor when the child classes are instantiated?

Note:

In multi-inheritance, the constructor is centered on the first parent class. If the subclass re-defines the constructor, it will not call the constructor of the parent class. The constructor will only call the constructor of the first parent class. If the parent class has a method of the same name, this method called by subclass instance object is also the method in the first parent class. Of course, if you need to use the constructor of one or several parent classes in the subclass, you need to call the constructor of the parent class explicitly.

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 must be called in the construction of its derived class.
  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 methods of the corresponding type. If it cannot find the corresponding method in the derived class, it starts to look for methods one by one in the base class. (Search for the called method in this class before finding it in the base class ).
  4. Subclass only inherits all the public attributes and methods of the parent class, and can also be called by the parent class name in the subclass. For private attributes and methods, subclass does not inherit, therefore, sub-classes cannot be accessed by the parent class name.

Instance:

Class Parent (object): # define parent class parentAttr = 100 def _ init _ (self): print "Calling parent constructor" def parentMethod (self ): print 'calling parent method' def setAttr (self, attr): Parent. parentAttr = attr def getAttr (self): print "Parent attribute:", Parent. parentAttrclass Child1 (Parent): # define child1 class def _ init _ (self): print "Calling child1 constructor" def childMethod (self): print 'calling child1 method' Parent. parentMethod (self) # Call the method of the base class. Therefore, add the parameter selfclass Child2 (Parent): # define child2 class def childMethod (self): print 'calling child2 method 'self. parentMethod () # subclass calls the method c1 = Child1 () of the parent class inherited from the parent class # instantiate subclass 1c2 = Child2 () # instantiate subclass 2c1. childMethod () # Call The subclass method c2.childMethod () # Call The subclass method c1.parentMethod () # Call the parent class method c1.setAttr (200) # Call the parent class method c1.getAttr () again () # Call the parent class method again

Execution result:

Calling child1 constructor
Calling parent constructor
Calling child1 method
Calling parent method
Calling child2 method
Calling parent method
Calling parent method
Parent attribute: 200

Resolution:

C1 = Child1 () # instantiate subclass 1c2 = Child2 () # instantiate subclass 2

These two statements are used to create instance objects of the Child1 and Child2 classes. When an instance object is created, the constructor of the class is automatically called. If the constructor does not have its own constructor, the constructor of the parent class is called, so the result is:

Calling child1 constructor
Calling parent constructor

C1.childMethod () # Call The subclass Method
C2.childMethod () # Call The subclass Method

The preceding two statements respectively call the subclass method of the corresponding subclass. The result is:

Calling child1 method
Calling parent method
Calling child2 method
Calling parent method

Note:

After the child class inherits the parent class, if the child class needs to use the method of the parent class when it modifies the method of the parent class, what should I do when the subclass wants to call the method of the parent class? There are two methods:

  1. Parent. parentMethod (self)
  2. Self. parentMethod ()

The first type is to directly use the class name of the parent class. the method name calls the method of the parent class. However, when you call this method, you must pass self as a parameter and use it as the first parameter to point to the instance of this class.

The second is to directly use self to call the method of the parent class. Why can this method be called like this? Once the subclass inherits the parent class, the subclass has all the methods and attributes of the parent class, so the methods and attributes of the parent class are equivalent to the subclass itself, therefore, you can directly use self to call the instance method without passing in the self parameter.

Use the issubclass () or isinstance () method to detect the relationship between classes:

  1. Issubclass ()-Boolean function determines whether a class is a subclass or Child class of another class. Syntax:issubclass(sub,sup) .
  2. The Boolean function of isinstance (obj, Class) returns true if obj is an instance object of the Class or an instance object of a Class subclass.

Summary

The above is all the details about the Python Object-oriented Inheritance code in this article, and I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.