Single inheritance and multiple inheritance in Python

Source: Internet
Author: User
This article mainly introduces the single inheritance and multiple inheritance in Python, combined with an example, it analyzes the concept, principle, realization method of single inheritance and multi-inheritance in Python object-oriented programming, and the related operation considerations, and the friends who need can refer to

The examples in this article describe single inheritance and multiple inheritance in Python. Share to everyone for your reference, as follows:

Single inheritance

First, Introduction

Python also supports the inheritance of classes, and if a language does not support inheritance, the class has little meaning. The definition of a derived class is as follows:

Class Derivedclassname (BaseClassName1):  <statement-1>  ...  <statement-N>

Note the order of the base classes in parentheses, if the base class has the same method name, and if it is not specified when the subclass is used, the python left-to-right search is not found in the subclass, and left-to-right finds whether the method is contained in the base class.

Baseclassname (the base class name in the example) must be defined within a scope with the derived class.
In addition to classes, you can also use expressions, which are useful when a base class is defined in another module:

Class Derivedclassname (ModName. Baseclassname):

Second, the Code

#-*-Coding:utf-8-*-#! Python3class people:  #定义基本属性  name = ' Age  =0  #定义私有属性, private properties cannot be accessed directly outside the class  __weight =0  #定义构造方法  def __init__ (self,n,a,w):    self.name = n    self.age = a    self.__weight = w  def speak (self):    Print ("%s" says: I'm%d years old.) "% (self.name,self.age))    #单继承示例class Student (people):  grade ="  def __init__ (self,n,a,w,g):    # Call the constructor of the parent class, which can be #people in the following two ways    . __init__ (self,n,a,w)    super (). __init__ (n,a,w)    self.grade = G    # Override method for parent class  def speak (self):    print ("%s said: I'm%d years old, I'm reading grade%d"% (self.name,self.age,self.grade)) s = student (' Ken ', 10,60,3) S.speak ()

Iii. Results of operation

Ken says: I'm 10 years old, I'm in Grade 3.

Multiple inheritance

First, Introduction

Python also has limited support for multiple inheritance forms. The class definition for multiple inheritance is as follows:

Class Derivedclassname (Base1, Base2, Base3):  <statement-1>  ...  <statement-N>

Note the order of the parent class in parentheses, if the parent class has the same method name, and if it is not specified when the subclass is used, the python left-to-right search means that the method is not found in the subclass, from left to right, to find whether the method is contained in the parent class.

Second, the Code

#-*-Coding:utf-8-*-#! The python3# class defines class people:  #定义基本属性  name = ' Age  =0  #定义私有属性, private properties cannot be accessed directly outside the class  __weight =0  # Definition construction Method  def __init__ (self,n,a,w):    self.name = n    self.age = a    self.__weight = w  def speak (self):    Print ("%s" says: I'm%d years old.) "% (self.name,self.age)) #单继承示例class Student (people):  grade ="  def __init__ (self,n,a,w,g):    #调用父类的构函    people.__init__ (self,n,a,w)    self.grade = g  #覆写父类的方法  def speak (self):    print ("%s" said: I'm%d years old, I'm reading grade%d "% (Self.name,self.age,self.grade)) #另一个类, pre-Multiple Inheritance Preparation class speaker ():  topic = '  name = '  def __ Init__ (self,n,t):    self.name = n    self.topic = t  def speak (self):    print ("My name is%s, I am an orator, I am speaking the topic%s" % (self.name,self.topic)) #多重继承class sample (Speaker,student):  a = "  def __init__ (self,n,a,w,g,t):    student.__init__ (self,n,a,w,g)    speaker.__init__ (self,n,t) test = sample ("Tim", 25,80,4, "Python") Test.speak () # Method name, which is called by default in parentheses before the parent class.

Iii. Results of operation

My name is Tim, I'm an orator, and the subject of my speech is Python.

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.