The Inheritance of object-oriented programming

I. Introduction

  1, build a new kind of inheritance is under way to class, parent class can be called a base class or super class, the new class can be called subclass or derived class, subclass can inherit the properties of the parent class.

  2, the parent class may be inherited by subclasses, subclasses can inherit also a plurality of parent classes.

  3, in python3, the highest ancestor class inherits the default built-in object class.

  4, in python2, the highest ancestor class does not inherit default, this time, the class and its descendant classes are classic classes. Unless explicitly highest ancestral parent class is the built-in object, that class and its descendants are the new class to class.

class GreatGrandfather:
    X = 111
 class Grandfather (GreatGrandfather):
     Pass 
class Father (Grandfather):
     Pass 
class Son (Father):
     Pass 
son_obj_1 = Son ()
 Print (GreatGrandfather. __bases__ )   # to python3 result (<class 'object'>, ) python2 Results of () 
Print (son_obj_1.x)
 Print (Son.x)
 Print (Father.x)
 Print (Grandfather.x)
 # The above results are 111

 

Second, multiple inheritance: a subclass inherits more than one parent class

  1, the advantages of: subclass can use the properties of each parent class, improve the utilization of the code

  2. Disadvantages:

    ① from thinking logically, this child belongs to the class itself defines a "category" has become blurred.

    Readability and scalability ② code worse, it may lead to "diamond problem."

  3, if inevitably require the use of multiple inheritance way, should Mixins method.

Third, resolve inheritance code redundancy

class Employee:
    Gender = ' MALE '   # subclass total data attributes 
    DEF  the __init__ (Self, name, Age):   # a subclass object instance of common attributes 
        the self.name = name
        self.age = Age
     DEF SHOW_NAME (Self):   # subclasses common functionality 
        Print ( ' which employees} { ' .format (the self.name))
 class the Worker (the Employee):
     DEF work_time (Self, Time):   # the Worker sub class specific features 
        Print ( ' {} today's work is: {} ' .format (the self.name, Time))
 class Manager (the Employee):
     DEF  the __init__ (Self, name, Age, skill):
        . The Employee the __init__ (Self, name, Age)   # when subclass Manager object instance, if the parent has direct references 
        self.skill = skill   # parent is not, and then definable 
    DEF work_target (Self, target):   # Manager subclass specific features 
        Print ( ' {} month indicators: {} ' .format (the self.name, target))
worker_obj_1 = Worker('tom','18')
worker_obj_1.show_name()
worker_obj_1.work_time('8h')
manager_obj_1 = Manager('jan','20','dance')
manager_obj_1.show_name()
manager_obj_1.work_target ( ' 10 Yi ' )
 # result 
'' '
This is tom employees
tom work today is: 8h
This is a staff jan
jan indicators this month: 1000000000
'''

Fourth, property search order

  1, the case of single inheritance: because it is a linear relationship, simply follow seniority can find small to large.

  2, multiple inheritance situations:

 

Guess you like

Origin www.cnblogs.com/caoyu080202201/p/12669110.html