"Python"--Multiple inheritance of classes, classic class, Modern class

Source: Internet
Author: User

Inheritance Knowledge Point Supplement

Python also supports multiple inheritance, but generally we seldom use, some languages simply do not support multiple inheritance, there are multiple inheritance, will bring two concepts, classic class and New class.

One, multiple inheritance

Before we were all talking about single inheritance, then what is multiple inheritance? To be blunt, that is: Subclasses can inherit multiple parent classes, called multiple inheritance.

classSchoolmember (object):#Schoolmember class    " "School Members base class" "    defTell (self):Print("The schoolmeber is ...") classSchool (object):#School Class    """School Class"""    defOpen_branch (self,addr):Print("openning A new branch in", addr)classTeacher (Schoolmember,school):#Subclass teacher Inherits Schoolmember,school two classes at the same time    "Lecturer Class"    defTeaching (self):"Lecture Methods"        Print("Teacher Xiaogao is teaching python") T1=Teacher () T1.tell ( )#Tell method with parent class SchoolmemberT1.open_branch ("Shanghai")#Open_branch method with parent class school

Ii. New types of

1. Concept

The new class definition must inherit the object class, which is defined as inheriting the object class, which is called the new class

class Person (object):  # inherits the object class    " Modern class "

2. Inheritance Construction method

New class initialization construction method with super keyword to inherit

Super (subclass, self). __init__ (Name,age)

3. Call the same method in the parent class or the same property in the order

The new type of multi-inheritance invocation method is sequential: breadth-first query, such as:

The code experiment is as follows:

① All Codes

classA (object):#New Class    def __init__(self): SELF.N="A"classB (A):def __init__(self): SELF.N="B"classC (A):def __init__(self): SELF.N="C"classD (b,c):def __init__(self): SELF.N="D"D=D ()Print(D.N)#Outputd All Code
View Code

First find the properties of your own class, output D

② Comment code in Class D

classA:def __init__(self): SELF.N="A"classB (A):def __init__(self): SELF.N="B"classC (A):def __init__(self): SELF.N="C"classD (b,c):PassD=D ()Print(D.N)#OutputB
View Code

③ code in the class B comment

classA:def __init__(self): SELF.N="A"classB (A):PassclassC (A):def __init__(self): SELF.N="C"classD (b,c):PassD=D ()Print(D.N)#OutputA
View Code

④ Comment code in Class C

 class   A (object):  def  __init__  =  " a  "  class   B (A):  pass  class   C (A):  pass  class   D (b,c):  pass  d  =  D ()  print   (D.N)  #   output  A 
View Code

Third, the classic class

1. Concept

Classic class definition, nothing inherits

class Person :     " Classic Class "

2. Inheritance Construction method

The parent class. __init (Self,name,age)

3. Call the same method in the parent class or the same property in the order

The classic class multiple-inheritance invocation method is in order: depth-first query, such as:

The code experiment is as follows:

① All Codes

classA:#Classic Class    def __init__(self): SELF.N="A"classB (A):Pass    def __init__(self): SELF.N="B"classC (A):def __init__(self): SELF.N="C"classD (b,c):def __init__(self): SELF.N="D"D=D ()Print(D.N)#OutputD
View Code

② Comment code in Class D

classA:def __init__(self): SELF.N="A"classB (A):def __init__(self): SELF.N="B"classC (A):def __init__(self): SELF.N="C"classD (b,c):PassD=D ()Print(D.N)#OutputB
View Code

③ code in the class B comment

classA:def __init__(self): SELF.N="A"classB (A):PassclassC (A):def __init__(self): SELF.N="C"classD (b,c):PassD=D ()Print(D.N)#OutputA
View Code

④ Comment code in Class A

 class   A:  pass  class   B (A):  pass  class   C (A):  def  __init__   =  " c  "  class   D (b,c):  pass  d  = D ()   Print   (D.N)  #   output  C 
View Code

Summarize:

    1. The new class inherits the object class, and the classic class does not inherit any classes
    2. The new class inherits the construction method with the Super keyword, and the classic class inherits from the parent class. __init (self).
    3. New class: Breadth-first query, Classic class: Depth-first query (because the new class is about the newer, so look for the latest, newest, and then the classic of the ancient, so far deeper)
    4. It is noteworthy that the above is done in the Python2, in the Python3, whether it is the classic class or the new class, are using breadth-first query, has been discarded 2 in depth query

"Python"--Multiple inheritance of classes, classic class, Modern class

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.