Inheritance in python

Source: Internet
Author: User

1. python defines a class method:

class Animal():def __init__(self,name):self.name=name;def show(self):print self.name;a=Animal('dog');a.show();

An Animal class is defined above. It starts with the class keyword. Methods in the class start with the def keyword. init in the class is the class constructor, which is automatically called when an instance is generated, the show method is also defined in the class, which is used to print the instance variable name. Each method has the parameter self, which can be considered as the current object itself.

2. inheritance is an important feature of object-oriented systems. It can improve code reusability and reduce code rewriting. Inheritance can be divided into single inheritance and multi-inheritance. In python, the method used to specify the base class is to write the name of the base class to the brackets when defining the derived class, such as class derive (base). python can use two single inheritance methods. The first one is:

class Animal():def __init__(self,name):self.name=name;def show(self):print self.name;#a=Animal('dog');#a.show();class dog(Animal):def __init__(self,name):Animal.__init__(self,name);def bark(self):print "dog can bark";d=dog('dog');d.bark();

The above dog class inherits from the base class Animal. Its constructor calls the base class's constructor Animal. _ int _ (); note that when a python derived class instantiates an object, it does not automatically construct the base class. The object of the derived class first searches for the init method in this class. If it finds it, it calls it and completes the instantiation. If it cannot find it, it searches for the init method in the base class to complete the instantiation. If there are multiple base classes, the init method will be searched in order of inheritance, which will be discussed later.

The second method of single inheritance is to use the super function. The method is as follows:

__metaclass__=type;class Animal():def __init__(self,name):self.name=name;def show(self):print self.name;#a=Animal('dog');#a.show();class dog(Animal):def __init__(self,name):super(dog,self).__init__(name);def bark(self):print "dog can bark";d=dog('dog');d.bark();

The above dog class uses super (dog, self). It accepts two parameters: the derived class name and self.

3. Multi-inheritance of python

For multi-inheritance, you only need to fill in two or more base classes in brackets, such as class derive (base1, base2)

class base1():def __init__(self):print "base1 called";class base2():def __init__(self):print "base2 called";class derive1(base1,base2):def __init__(self):#base1.__init__(self);#base2.__init__();print "derive1 called";class derive2(base2,base1):pass;

Type the following command:

>>> D1 = derive1 ()

Derive1 called

Delete the _ init _ (self) method in derive1 and run the following command:

>>> D1 = derive1 ()

Base1 called

>>> D2 = derive2 ()

Base2 called

>>> D1.show ()

This is base1

>>> D2.show ()

This is base1

When a derived class is instantiated, it first looks for the constructor in this class. If so, it calls the constructor of this class. If not, it calls the first method that calls the constructor of the base class, if the preceding derive1 does not define a constructor, The base1 constructor is called. In addition to the constructor method, other methods call this method. For example, d1.show () calls the show () method in base1.


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.