In software design, code reuse and code stability have always been one of the important goals of software development, because it is the only way to accumulate a variety of basic components and maintain the work done before. From the reuse of features, is to use the work done before, such as the IC in the hardware, do not need to know how it is implemented, as long as the use of it on the line. So the software also needs to have this IC, in the code level of the IC, in the past seems to only use the way of function reuse, or just data structure reuse. When the Object-oriented programming era, naturally think of the use of class-use, so that data structures and functions can be reused at the same time, than the function of reuse more worry a step, is the data structure and algorithms do not need to care about, only care about the class provides a few interfaces. For example, want to MD5 for signature, only use this class to set the calculated data and length, you can directly return the corresponding MD5 signature. And do not care about what the internal operations, the use of what data structure to save the intermediate variables. In the reuse of classes, there are two main ways, one is to use the class directly, do not make any changes to the class, such as the combination of classes, and the other is to change the class, inheritance is the main way. Inheritance is more convenient than a combination of methods, because inheritance can directly use the data structure of the parent class, without taking into account the life cycle of the parent class, and the way in which the combination is to be considered. Inheritance can be done without changing the previous interface and using the latest optimized algorithm, and the combination is necessarily changing the interface. inherited ways to handle different versions of software compatibility are also handy, for example, the development of three versions of the software, each version of the functional test completed, and then develop the next version, in the development of the next version, do not want to change the original code, but also to use and modify some of its calculation methods, the use of inheritance is the best. This enables the automatic recognition of different versions of the function in a class, which can be created only by creating a new class object.
Python also has the design of class inheritance, its class inheritance way how? Generally as follows:
Class Super:
def __init__ (self, x):
print (' Super init ')
class Sub (Super):
def __init__ (self, x, y):
super.__init__ (self, x)
print (' Sub init ')
I = Sub (1, 2)
As you can see from the above, the Super class is a base class (also known as a superclass) and a sub is an inherited super class. A sub class can inherit multiple base classes, as long as the parentheses are tied more than one line, such as Sub (Super1, Super2, Super3). To explicitly call the constructor of a base class in a derived class, you can run a base class constructor, such as super.__init__ (self, x). This is not like C + +, in C + + is automatically run the base class constructor, and then run the constructor of the derived class. Python is customized as needed, and whenever you want to run it is called somewhere.
Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/