Object-oriented core is object, object is instantiated by class, then there is an inheritance relationship between class and class, the inherited class is called parent class, and the class of parent class is subclass.
The subclass inherits the parent class, and the subclass-instantiated object can call the methods of all the parent classes and, of course, all the methods of the subclass itself. Because these methods belong to the method of the object.
For example, child subclass inherits the parent class father
child.py
From father import Father
class child (father):
def childprint (self): print ' This is ' child
'
father.py
Class Father:
def Common (self):
print "This is a father also common"
Then one instance of the subclass, test = child (), can call the common () method of the parent class, as follows:
third.py
From child import child
test = child ()
Test.common ()
The result of running third.py is:
But if we make the following modifications to the father.py file:
father.py
Class Father:
def Common (self):
print "This is a father also common"
self.childprint ()
The results of the execution are as follows:
As you can see, "This is a child" was also printed. It is obvious that an instance of our subclass invokes the common method of the parent class, which actually calls the Childprint method of the subclass. What's going on.
The fact is, as I said before, when a subclass is instantiated, the method of the parent class inherited by all subclasses and the methods of the subclass itself are all owned by that instance, and then the common method is naturally the object that the subclass just instantiated.
All of the childprint methods that nature can invoke subclasses in the common method. On the surface, the parent class invokes the subclass method, or the subclass instance invokes its own method. Self in Self.childprint () refers to a subclass instance.