29, inherited

A succession

1.1 What is inherited

  Inheritance is a way to create a new class, a new class can be called the child class or derived class, parent class can be called a base class or super-class, sub-class will be genetic parent class
1.2, Python Inheritance

  python support multiple inheritance relationship, a subclass can inherit one or more parent classes

class Parent1(object):
    the X- = 1111
 class parent2 (Object):
     Pass 
class Sub1 (Parent1):                                         # single inheritance 
    Pass 
class Sub2 (Parent1, parent2):                             # multiple inheritance 
    Pass 
Print (Sub1. __bases__ )
 Print (Sub2. __bases__ )
 Print (Sub1.x)

1.3, python2 and inherited differences python3

  python2

    New class type: a subclass can inherit the object class

    Classic style: no subclasses object class

  python3

    python3 only new-style class, it does not inherit any class will inherit the default object class
1.4, multiple inheritance in python

  advantage:

    Subclasses can inherit more than one parent class, the greatest degree of code reuse restrictions
  disadvantages:

    1, are contrary to human habit of thinking, expression of Inheritance is a relationship between what is what

    2, the code less readable

    3, does not recommend the use of multiple inheritance, there would be willing to initiate diamond problem, poor scalability
  Note:

    If unavoidable need to use a plurality of properties of the parent class, should be used mixins

1.5 Why use inheritance

  Solve the problem of code redundancy between class and class

1.6, how inheritance

  When the code are in the subclass needs parent class can be directly referenced

  When the code portion subclass needs only when the parent class, writing a class, then references by name from the parent class code example:

  def __init__(self, name, age, sex, salary, level):               类

  OldboyPeople.__init__(self,name,age, sex)                    

class OldboyPeople:                                     # common parent code 
    School = ' Oldboy ' 
    DEF  the __init__ (Self, name, Age, Sex):
        self.name = name
        self.age = age
        self.sex = sex

class Student (OldboyPeople):                        # subclass code and the same parent class code from the parent class directly references code 
    DEF choose_course (Self):
         Print ( ' Student% s is elective ' % the self.name)
stu_obj = Student('lili', 18, 'female')
stu_obj.choose_course()

class Teacher (OldboyPeople):                       # subclass code, only part of the functions of the parent class in the      empty object teacher, 'egon', 18, ' male', 3000,10 class data write full 
    DEF  the __init__ (Self, name, Age, Sex, salary, Level):                 
    
# name names now go to the parent class OldboyPeople __init__ OldboyPeople. __init__ (Self, name, Age, Sex) self.salary = salary self.level = level DEF Score (Self): Print ( ' teacher was giving her pupils scoring% s ' % self.name) tea_obj=Teacher('egon',18,'male',3000,10) tea_obj.score()

Second, look for inherited property

2.1, the arrival of inheritance, objects when looking for property, they will start their __dict__ inside to find, if not go back and find the subclass, and finally to the parent class

  Example a: the subclass is not found, found in the parent, the parent class functions erupted class and the parent class has, directly reads the start subclass  

class Foo:
    def f1(self):
        print('Foo.f1')

    def f2(self):
        print('Foo.f2')
        self.f1() # obj.f1()

class Bar(Foo):
    def f1(self):
        print('Bar.f1')

obj = Bar ()
obj.f2()                           #Foo.f2    Foo.f1

The case of 3.2, a subclass can not be found, found in the parent class, then the parent class will directly point to a subclass, if you are forced to point to the parent class, you need to specify the name of the road call the parent class functions: Foo. f1 (self)

class Foo:
    def f1(self):
        print('Foo.f1')

    def f2(self):
        print('Foo.f2')
        Foo.f1 (Self) # call in the current class f1

class Bar(Foo):
    def f1(self):
        print('Bar.f1')

obj = Bar ()
obj.f2()

# Foo.f2
# Foo.f1

3.3, when called from the parent class, class encounter hidden attribute, the name will be distorted, but not within the outside, so the find function from the parent class

class Foo:
    def __f1(self): # _Foo__f1
        print('Foo.f1')

    def f2(self):
        print('Foo.f2')
        Self. __f1 () # self._Foo__f1, # f1 call in the current class

class Bar(Foo):
    def __f1(self): # _Bar__f1
        print('Bar.f1')

obj = Bar ()
obj.f2()

# Foo.f2
# Foo.f1

Fourth, the diamond multiple inheritance problems caused by relationship

4.1 Introduction diamond issues and MRO

  Diamond question refers to multiple parent classes, parent eventually point to the same parent

  MRO: refers to the access attribute search sequence according to the object class is to display MRO

4.2, search order

  Non-diamond:

    According to the order of the parent class subclass left to right, a branch of a branch looking down,

  diamond:

    Classic: depth-first, each branch to find in the end, until the final parent, the second branch will not find the ultimate parent

    New Type: breadth-first, the last branch will find the ultimate parent, the other parent will skip

class G: # in the python2 not inherit the object class and its subclasses are Classic 
# DEF Test (Self):
# Print ( 'from G')
Pass

class E (G):
# DEF Test (Self) :
# Print ( 'from E')
Pass

class F. (G):
DEF Test (Self):
Print ( 'from F.')

class B (E):
# DEF Test (Self):
# Print ( 'from B')
Pass

class C (F.):
DEF Test (Self):
Print ( 'from C')

class D (G):
DEF Test (Self):
Print ( 'from D')

class A (B, C, D):
# Test DEF (Self):
# Print ( 'from A')
Pass

# new class
# print (A.mro ()) # A-> B->E->C->F->D->G->object

# Classic: A-> the B-> E-> G-> the C-> F-> D
obj = A ()
obj.test () #

4.3 summary

  Or to use multiple inheritance?

  Use, but several issues need to avoid

  1. Do not be too complicated inheritance structure

  2. Recommended use mixins mechanism to meet the multiple inheritance in the context of what is the relationship between what

Guess you like

Origin www.cnblogs.com/jingpeng/p/12669830.html
29