Three features of OOP inheritance

  • inherit

  • What is inherited: the relationship between the two objects, called a subclass, called a parent class, which subclasses inherit the properties and methods of the parent class skills, it can be used directly. ( It is worth noting: all created in python3 class directly or indirectly, implicit or explicit inherited object class, If you do not inherit a class, the default inherited object class, also known as the new class)

  • Why use inheritance: reduce code reuse

  • How to represent in code

  • class Base: 
        Character   = ' I am a parent class ' 
        DEF want_basketball (Self): \
             Print ( ' coach I want to play basketball ' ) 
    
    class SubClass (Base):                # by adding in parentheses subclass the parent class name 
        Pass 
    
    obj = SubClass () 
    obj.want_basketball ()                # method to directly call the parent class's properties and methods if exactly the same, then 
    Print (obj.character)

    For example, good and bad, yellow, white, black people in this category are the inheritance , but they are abstract and adult students of this class.

  • Then when the child class and parent class has properties and methods of conflict, is not inherited methods can not do? Obviously not, in general, we can be a common feature of the parent class and subclass are extracted, professional terms can be called abstract, abstract finished as a new class of common categories, sub-categories and above for parent use .

  • Take football and basketball players, for example, for them are all human beings, we have a name and gender characteristics such as height, but they are not the same skills
  • class the Person:
         DEF  the __init__ (Self, name, height, Gender):           # defines an abstract class common to have 
            the self.name = name 
            self.height = height 
            self.gender = Gender 
    
    class FootBallPlayer (the Person):                        # by subclass name in parentheses which add an abstract parent class name inherited the role played 
        DEF Skill (Self):
             Print ( ' % S will play football ' % (self.name)) 
    
    class BasketBallPlayer (the Person):
         DEF Skill1 (Self):
             Print ( ' % S play basketball ' %(self.name)) 
    
    
    
    Kobe = BasketBallPlayer ( ' kobe Bryant ' , ' 189 ' , ' MALE ' )      # Name Height and other information you do not need to call the parent class by directly calling the subclass 
    Kobe.Skill1 () 
    Messi = FootBallPlayer ( ' Messi Lion ' , ' 176 ' , ' MALE ' ) 
    Messi.Skill ()

     

  • Find properties exist after the inheritance

  • Bound to a class inherits from another class is inherited class may also inherit other classes, inheritance corresponding to C B, B A and Inheritance

  • Find a property at this time the order is:

    The object itself namespace -> class namespace -> parent class namespace -> parent of the parent class namespace -> ... object class

    I will always look back along the inheritance, until you find, because the object is the root of all classes, so if the object could not find the final will look like!

  • class Foo:
        def f1(self):
            print('Foo.f1')
    
        def f2(self):
            print('Foo.f2')
            self.f1()
    
    class Bar(Foo):
        def f1(self):
            print('Bar.f1')
    
    
    b=Bar()
    b.f1()
    #输出 Bar.f1
    b.f2()
    #输出 Foo.f2
  • Derived and coverage

  • Derived definitions: when the parent class attributes provided when not fully meet the needs of subclasses , subclasses can add their own property or method, or covering the parent attribute that already exists, then the parent class is called a subclass derived class ;

  • Defined coverage: If the subclass appear in the parent class's attributes and methods of the same, to be called, it will give priority subclass attributes.
  • Class erupted in many cases the code in the parent class is different only a small part, but it had to define a new subclass, this time you can already call the parent class method in a subclass to complete most of the work, child just write a different kind of a small portion of the parent class code to

  • There are two ways and methods of the parent class attributes can be reused

  • Use the class name called directly, the way has nothing to do with inheritance, there is no immediate inheritance, you can also call
  • Use super ()
  • class the Person:
         DEF  the __init__ (Self, name, height, Gender):           # defines an abstract class common to have 
            the self.name = name 
            self.height = height 
            self.gender = Gender 
    
    class FootBallPlayer (the Person):                        # by subclass name in parentheses which add an abstract parent class name inherited the role played 
        DEF  __init__ (Self, name, height, Gender, Field,):     # If you do not write These two lines can not call the name height gender attributes 
            Super (). __init__ (name, height, Gender)         # reuse code parent class, preventing covering parent class init method 
            self.field = Field
         DEFSkill (Self):
             Print ( ' % S play football ' % (the self.name))
             Print (self.height, self.gender, self.field)
    Messi = FootBallPlayer('Lion Messi','176','male','grass')
    Messi.Skill()

     

  • combination

  • When there is no significant link between the two classes, or that they are not the same, when an object is to use the property of another object , this concept called combinations.

  • class Equip:   # weaponry class 
        DEF Fire (Self):
             Print ( ' Release Fire skill ' ) 
    
    
    class Riven:   # hero Riven class, a hero needs to have the equipment, thus requiring a combination Equip class 
        of cAMP = ' Noxus ' 
    
        DEF  __init__ (Self , Nickname): 
            self.nickname = Nickname 
            self.equip = Equip ()   # generates equipped with Equip a class assigned to the attribute instance equip 
    
    
    R1 = Riven ( ' sharp Wenwen ' ) 
    r1.equip.fire ()   # can be used grouped objects held by the method produces at
  • Realization of the principle of inheritance

 

  • class A(object):
        def test(self):
            print('from A')
    
    class B(A):
        def test(self):
            print('from B')
    
    class C(A):
        def test(self):
            print('from C')
    
    class D(B):
        def test(self):
            print('from D')
    
    class E(C):
        def test(self):
            print( ' From E ' ) 
    
    class F. (D, E):
         # DEF Test (Self): 
        #      Print ( 'F. From') 
        Pass 
    F1 = F. () 
    F1.test () 
    Print (F. __mro__ ) # only until new this property can have a linear list view, this is not the classic class attribute 
    
    # new class order of succession: F-> D-> B-> E-> C-> A 
    # classic order of succession: F-> D-> B-> A-> E-> C 
    # python3 are unified in the new class 
    # pyhon2 in Caifen new classes and class classical 
    
    order of succession
  • Inherit the essence

  • python in the end is how to achieve inheritance, for each class you define, calculate a python accounting method resolution order (MRO) list, the MRO list is a simple list of all the linear sequence of base classes, for example,
  • >>> F.mro() #等同于F.__mro__
    [<class '__main__.F'>, <class '__main__.D'>, <class '__main__.B'>, <class '__main__.E'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>
  • All the parent class MRO list and follow the following three criteria:

    1. subclasses are checked before the parent

    2. A plurality of parent classes based on their order in the list is checked

    3. If there are two legal option for the next class, selecting a first parent

 

Guess you like

Origin www.cnblogs.com/ITchemist/p/11247632.html