Object-oriented: in the footsteps (inheritance)

Object-oriented: in the footsteps (inheritance)

1. What is object-oriented inheritance

Inheritance is a concept of object-oriented software technology among. If a category A "inherited from" Another category B, put the A is called "sub-category B", and B is called the "father of category A" can also be called "B of A is a superclass." Inheritance can have various subcategories such properties and methods of the parent class, without the need to write the same code again. While Reiko category inherit the parent class, you can re-define certain attributes, and override some methods, properties and methods of the original parent category that is covered, so to get the parent category different functions. In addition, for the subcategory additional new properties and methods it is also common practice. General static object-oriented programming language, inherited a static, meaning it has been decided to compile in the behavior of sub-categories, can not expand in the implementation period.

Simply put, all through his father's footsteps, the legal successor to the assets, if you are an only child, so nothing else you will inherit all the property of your parents.

Simple inheritance example:

class Animal:
    def __init__(self,name,age,sex):
        self.name=name
        self.age=age
        self.sex=sex
class Dog:
    def __init__(self,name,age,sex):
        self.name=name
        self.age=age
        self.sex=sex
#继承
class Animal:
    def __init__(self,name,age,sex):
        self.name=name
        self.age=age
        self.sex=sex
class Dog(Animal):
    pass
2. inherited characteristics
  • It increases the coupling classes and class

  • Reduce duplication of code

3. inherited classification

In the above example

Animal said parent class, the base class, superclass

Dog said subclass, derived class

inherit

  • Single inheritance

  • Multiple Inheritance

4. single inheritance

Simple usage

class Animal:
    def __init__(self,name,age,sex):
        self.name=name
        self.age=age
        self.sex=sex
class Dog(Animal):
    def run(self):
        print(f"{self.name}正在跑。。。")
dog1=Dog('大黄',8,'')
dog1.run()

In this example, dog belonging to the animal, and the animal have a common attribute name, age, sex, if we each animal class initialization name, age, sex code is redundant and inefficient.

The method of simultaneously performing class and parent class

class Animal:
    def __init__(self,name,age,sex):
        self.name=name
        self.age=age
        self.sex=sex
class Dog(Animal):
    def __init__(self,name,age,sex,speed):
        super().__init__(name,age,sex)
        self.speed=speed
    def run(self):
        print(f"{self.name}正在以{self.speed}迈的速度跑。。。")
dog1=Dog('大黄',8,'Mother ' , 35 ) 
Dog1.Run ()
5. Multiple Inheritance

Man of few words said on the code, their own taste

class Fairy: # fairy class 
    DEF Fly (Self):
         Print ( ' fairy fly ... ' )
 class Monkey: # Monkey class 
    DEF EAT (Self):
         Print ( " monkey steal peach eat !!! " )
 class SunMonkey ( Fairy, Monkey):
     Pass 
Sun1 = SunMonkey () 
sun1.fly () 
sun1.eat ()

Read Journey to the West all know, the Monkey King is both a monkey god, in the above code, he naturally inherited the two methods in this class is a simple multiple inheritance.

That being inherited two classes, it should relate to the order of succession issues.

The new class of multiple inheritance

We should add categories like python:

There are two classes in python2x version:

  • ⼀ a called Classic . Before python2.2. ⼀ Use a straight Classic. Classic if nothing is written in the root base class.

  • ⼀ called a new class . The new class emerged after python2.2. Features new class is the base class is the root class object.

python3x version only one class:

  • Use python3 manipulation is the new class . If the base class who do not inherit. That this class will inherit the default object

mro sequence -C3 algorithm

mro is an ordered list, created in the class is calculated on the

Spoken official

MRO (Child (Base1, Base2)) = [Child] + Merge (MRO (of Base1), MRO (Base2 are looked up), [of Base1, Base2 are looked up]) 
(where Child inherited from Base1, Base2)

Which merge operation is the core algorithm c3

Here are some of the knowledge about the formula

Header and footer

  • Header - the first element of the list

  • Footer - a collection of tables listing the elements other than the head, (possibly empty)

Let me show you to calculate a calculation process

class A(object):
    passclass B(A):
    passclass C(A):
    passclass D(A):
    passclass E(B,C):
    passclass F(C,D):
    passclass G(D):
    passclass H(E,F):
    passclass I(F,G):
    pass

 

Formula: 

MRO (K (H, the I)) = [K] + Merge (MRO (H), MRO (the I), [H, the I]) 
is calculated MRO (H) - seen from the above code Class H Inherited E, F two classes 
mro (H (E, F)) = [H] + Merge (mro (E), mro [F.], [E, F]) 
then calculates mro (H) in mro (E), MRO (F.) 
MRO (E (B, C)) = [E] + Merge (MRO (B), MRO (C), [B, C]) 
        has seen the code class B inherits from the class A
         = [E] + Merge ([B, A], [C, A], [B, C])
 # thus sequentially calculated mro (E), mro (F ) and then calculate mro (H), in accordance with this idea is calculated mro (I) .................. 
finally obtained: 
    MRO (K (H, the I)) = [K] + Merge ([H, E, B, F, C, D, A], [I, F, C, G, D, A], [H, I]) 
and then in accordance with the merge operation 
finally calculate the results are as follows: 
    [K, H, E, B, I, F, C, G, D, a] 
which is the order in which these class inheritance

Examples merge operation

Computing merge ([E, O], [C, E, F, O], [C]) 
has three lists: ① ② ③ 
Merge is not empty, to take the first list of the header list ① E, the determination                               
   each end of the table lists are [O], [E, F , O], E at the end of the set of these tables, thus skipping the current listing current 
extraction list table header ② C, judgment 
   C is not a collection of individual lists , and thus out to the outside Merge C, and removed from all header 
   Merge ([E, O], [C, E, F., O], [C]) = [C] + Merge ([E, O ], [E, F, O ]) 
carried out under a new merge operation ......
 ---------------------

This time it is calculated OK.

Finally, talk about the method of calculation provided python execution order

print(mro(A))

The results of the above code as follows

[<class '__main__.K'>, <class '__main__.H'>, <class '__main__.E'>, <class '__main__.B'>, <class '__main__.I'>, 
<class '__main__.F'>,
<class '__main__.C'>, <class '__main__.G'>, <class '__main__.D'>, <class '__main__.A'>, <class 'object'>]

This will see a lot of people wondering, since there python methods, which also derive doing it manually?

I would like to ask you, when you go to the written test, no computer you how to do it? So this will be a simple algorithm.

Guess you like

Origin www.cnblogs.com/z-x-h/p/12081023.html