Study notes --python (inherited)

Study notes (Python inheritance)

There are several popular name (parent - child class, the base class - derived class)
acquire code demonstrates this:

 1 class father:
 2     def work(self):
 3         print("work>>>>>")
 4 
 5     def car(self):
 6         print("car>>>>>>>>>")
 7 
 8 class son(father):   #想要继承就得添加父类
 9     def study(self):
10         print("study>>>>>>>>>>")
11           
12 obj = son()    
13 obj.work()    #In the above method, work is in the self parameter, obj at this time is directed 
14  # In addition it is also important, self always point to call methods of the caller (such as here pointing obj) 
15  # Work >> >>>

 

In this case it can be seen by the code calling the object obj subclass, but the parent class may be used, which is mainly due to the added subclass parent, if the parent class is not added, the method can not call the parent class thus being given, this form is called inheritance
(self always point to call methods of the caller)

1, when the parent class inheritance, the parent class if some do not want to call, you will need to rewrite their own in a subclass

(The following classes override the work method in the parent class, Forget the too restrictive)

. 1  class Son (Father):
 2      DEF Study (Self):
 . 3          Print ( " Study >>>>>>>>>> " )
 . 4  
. 5      DEF Work (Self):
 . 6          Print ( " s.work " )      # Prefer Inheritance write your own (rewrite) 
7  
8  # at this point, starting with the method of the subclass to find a method call, if the method is first invoked, if not call the parent class went 
9 obj = son ()
 10 obj.work ()     # s.work

 

2, when inherited from the parent class method, own subclass overrides but still want to execute the parent class is overridden, then perform the following operations:

1  DEF Work (Self):
 2      Print ( " s.work " )
 3      super (Son, self) .work ()    # square one: method calls super, super (name of the current class, self) parent is rewritten. method ()) 
4      father.work (self)         # Fang II: this method is the initiative to call the parent class method of self at this time we need to take the initiative to add, represents the parent class must carry out its object methods

3, python and C ++ as more than one parent class can inherit

 1 class uncle:
 2     def video(self):
 3         print("uncle.video")   
 4 class father(uncle):
 5     def work(self):
 6         print("father.work")
 7 class mother:
 8     def car(self):
 9         print("mother.car")   
10         
11 class son(father,mother):  
12     def study(self):
13         print("son.study") 

 

(1) more than one parent class inherits from left to right beginning Inheritance:

obj = a () 
obj.car ()     # mother.car

 

(Father in this case is to find a class CAR () method does not jump to the mother class)

(2) If the above inherit the parent class has a parent class, "has been looking for in the end," there is called, no inherited parent class to find the next method

obj = a () 
obj.video ()     # uncle.video

 

(3) The above two versions is not based on the final point to the same parent, and the third determination is now based on the final parent discuss the case where the same is carried out, as follows:

class aunt:
    def computer(self):
        print("aunt.computer")


class uncle:
    def video(self):
        print("uncle.video")


class father(uncle):
    def work(self):
        print("father.work")


class mother(aunt):
    def car(self):
        print("mother.car")


class son(father, mother):
    def study(self):
        print("son.study")

obj = son()
obj.computer()   #aunt.computer

 

The order of their execution as shown below:
Execution order
The red line is up to inherit the black line is the flow of the code execution
or with (1) (2) almost, but not the same, while the left has been called method is not found, and no have been looking for in the end, but returned before the last son subclass a parent, the mother began looking for class, and finally pointing aunt class and find their computer () method
((3) summary : If you have a common parent class, when looking for ways, not always find in the end, but in the end before the parent seeking to change the most primitive to the parent to look for, if only to find in the end can not find the parent class ) how a good understanding on how to memory

(4) self. The method (using sequential code execution)

(Some modified code below)

class uncle:
    def video(self):
        print("uncle.video")   
class father(uncle):
    def work(self):
        print("father.work")
        self.car()
    def car(self):
        print("father.car")    
class mother:
    def car(self):
        print("mother.car")   
        
class son(mother,father):  
    def study(self):
        print("son.study") 
obj = son()
obj.work()    #father.work  
              #mother.car  

 

When an object, because the mother did not like their methods, to find and execute in class father on a trip to the parent looking for ways, in the method as well as self.car (), it stands to reason that there should be the same class which will take precedence but the result is mother.car, the reason is very simple, when it comes to self. method (), it should return to its target (the object pointed to self, this code is obj) class called in to perform again call its methods ( for the above code, is called again obj.car (), and then start looking for the subclass method, again looking for ways in parent class, a parent class when the first mother, the mother has the class car () method, so a direct call is executed, over! )

(5) class __init __ () method is first to find the time to execute:

 1 class uncle:
 2     def __init__(self):
 3         print("uncle.init")
 4     def video(self):
 5         print("uncle.video")   
 6 class father(uncle):
 7     def __init__(self):
 8         print("father.init")
 9     def work(self):
10         print("father.work")
11 class mother:
12     def car(self):
13         print("mother.car")   
14         
15 class son(father,mother):  
16     def study(self):
17         print("son.study") 
18 obj = son()   #father.init

 

(The code above father class also inherits a parent class, they also have __init__ method, but the first traverse to the subclass father, so I have to call the init method in its class, whenever the implementation of the object of its class, only performed once met first method is __init__ )

Guess you like

Origin www.cnblogs.com/renio/p/12370151.html