Learning Notes (Python Inheritance)
There are several names (parent-child, base-derived)
Let's start with a code demonstration:
1 class father: 2 def work(self): 3 print("work>>>>>") 4 5 def car(self): 6 print("car>>>>>>>>>") 7 8 class son(father): #To inherit, you have to add a parent class 9 def study(self): 10 print("study>>>>>>>>>>") 11 12 obj = son() 13 obj.work() #In the above method, work In self Is a formal parameter, which points to obj 14 #What's more important is that self Always point to the caller of the calling method (such as here) obj) 15 #work>>>>>
From the code above, you can see that obj is an object that calls a subclass at this time, but you can use the method of the parent class. The main reason is that the parent class is added after the subclass. If you do not add a parent class, you cannot call the method of the parent class and get an error. This form is called inheritance
(self always points to the caller of the calling method)
1. When inheriting a method of a parent class, if you do not want to call some methods of the parent class, you need to override them in the child class yourself.
(Override the work method in the parent class in the following classes, don't think too much about restrictions first)
1 class son(father): 2 def study(self): 3 print("study>>>>>>>>>>") 4 5 def work(self): 6 print("s.work") #Write it yourself (rewrite) without inheriting it 7 8 #At this point, when calling the method, look first from the method in the subclass, if there is onebeforeCall, if not, go to the parent class to call 9 obj = son() 10 obj.work() #s.work
2. When inheriting a parent method, your child overrides it, but still wants to execute the method whose parent is overridden, do the following:
1 def work(self): 2 print("s.work") 3 super(son,self).work() #Party I: Call super Method, super(The name of the current class, self).Method whose parent class is overridden()) 4 father.work(self) #Square 2: This method is an active call to the parent method, at this time self Requires us to actively add, indicating that the parent also implements its object methods
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")
obj = son() obj.car() # mother.car
(Find the car() method in the father class first, then jump to the mother class if none)
(2) If there is a parent above the inherited parent, then "always search to the end", then call, or inherit the next parent to find a method
obj = son() obj.video() # uncle.video
(3) The above two statements are based on the discussion that the ultimate parent is not the same, and the third judgment is based on the discussion that the ultimate parent is the same, coded 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 sequence of execution is as follows:
Red lines inherit upwards and black lines are the process of code execution
It's similar to (1) (2), but unlike (1) (2), when the method invoked is never found on the left, it doesn't always find the bottom. Instead, it goes back to the son subclass before the last parent, starts looking in the mother class, and finally points to the aunt class and finds its computer() method
(3) Summary: If you have a common parent, you will not always search to the end when looking for a method. Instead, you will search for the parent before the end and change to the original parent before the end. If you can't find it, you will find the parent after the end.) How to understand and remember
(The code below modified a little)
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 is looking for a method in the de-parent class, since the mother class does not have its method, after it is found and executed under the father class, there is also self.car(),This method should reasonably take precedence in the same class, but the result is mother.car. The simple reason is that when a self.method () is encountered, it should be returned to the class called by its object (the object that self points to, this code is obj) to execute its method call (in the case of the above code, obj.car() is called again).Then look for a method from its subclass, and then look for a method from its parent. The first parent, mother, has a car() method in the mother class, so call execution directly, over!).
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 above code father class also inherits a parent class, which has both u init_u methods, but traverses to the subclass father first, so the init method under its class is called first, and only the first u init_u method encountered is executed once each time the object of its class is executed)