First, class and object
1. What is a class?
Classes, as the name implies, are collectively and abstractly the same kind of things that are specific to the same attributes.
Object, which is an instance of a concrete abstract class.
The above statement is not to look a little cloud in the fog to go. Yes, professional explanations are always hard to understand, and this is the expert. In fact, for a simple example, everyone will understand.
such as: Horse and Beast Sheep Chicken duck goose, these are collectively referred to as animals, right. The animal is the so-called class, and horse and Beast sheep and duck Goose is the corresponding instance of the class, that is, objects. Cows are objects, sheep are objects, ... Now you get it.
Python class is how to define and use, nonsense less say, at a glance
2. Members of the class: From what can be seen, the class contains a lot of members, and that class is what the classification of the members, see
3, the three major characteristics of the class: encapsulation, inheritance, polymorphism (Python uses very little polymorphism)
What is encapsulation: First look at a small problem, for example, to the database for the additional pruning check operation, then we can define 4 functions to implement these functions. But each time we call these functions we must pass parameters to these functions, we must tell them the database instance, user name, password, port number and other information. Such a, each invocation is to pass these same information. 1. Is there a better way to use this repetitive operation? 2, another for the function-oriented call, the fact that we do not know who actually used the function.
For object-oriented, the concept of encapsulation is actually used to solve the problems caused by such scenarios. In fact, we can put these common information in the arguments in the constructor, that is, when an object is instantiated, the object is given this information, and then the method's call does not have to repeat the arguments in the function. Call the method with the specified object to call these methods, you know who called, for example, it is clear
1>>>#function Implementation2 ... 3>>>defFetch (conn_info):4...Print "Use of %s conn to DB for Fetch"%Conn_info5 ... 6>>>defModify (conn_info):7...Print "Use of %s conn to db for modify"%Conn_info8 ... 9>>>#function calls, which need to pass in parameters every time they are calledTen... fetch ('Scott 123 127.0.0.1') OneUse of Scott 123 127.0.0.1 Conn to DB forFetch A>>> Modify ('Scott 123 127.0.0.1') -Use of Scott 123 127.0.0.1 Conn to DB forModify ->>> the>>>#class Implementation -...classA: -...def __init__(self,conn_info): -... Self.conn_info =Conn_info +...deffetch (self): -...Print "Use of %s conn to DB for Fetch"%Self.conn_info +...defModify (self): A...Print "Use of %s conn to db for modify"%Self.conn_info at ... ->>>#class calls -...#instantiates an object that indicates who (obj) is called, and the common parameter only needs to be passed once on instantiation . -.... obj = A ('Scott 123 127.0.0.1') ->>>Obj.fetch () -Use of Scott 123 127.0.0.1 Conn to DB forFetch in>>>obj.modify () -Use of Scott 123 127.0.0.1 Conn to DB forModify to>>>
4. Inheritance of classes
What is inheritance? For example, two people a and b,a can walk and eat and drink, B can also walk and eat and drink, and B can also draw, and a but not, then we can say B inherits from a
#Definition of inheritanceclassA:deffunc1 (self):Print "func1"classB (A):defFunc2 (self):Print "Func2"##实例化对象Obj2 =B ()##调用父类方法OBJ2.FUNC1 ()
The above code indicates that Class B inherits from Class A, is not very simple, and inherits the Class A (the parent class can be called by the object method of your class)
5, multiple inheritance: Then a class can only inherit from another class, whether it can inherit from more than one class, the answer is yes, this is Python-specific, other languages do not have this feature. The problem is, if a class D inherits from B,c two classes, b,c two classes inherit from Class A, Class B has no method Func,c class has method Func,a class also has method Func, that is called the Func method after Class D is called the method of which class?
For example, before looking at an example, let's say the classification of classes in Python (why?). Because the above problems are in two cases, the classic class and the new class are different, in Python, the class is divided into classic and modern classes, the definition of the new class only need to specify in the definition of the class inherits from the object class, as follows
# Classic Class class A: Pass # New Class class B (object): Pass
##经典类的多继承classA:deffunc (self):Print "Funca"classB (A):PassclassC (A):deffunc (self):Print "FUNCC"classD (b,c):Pass#instantiating an objectobj =D ()#function call, printing FuncaObj.func ()##新式类的多继承classA (object):deffunc (self):Print "Funca"classB (A):PassclassC (A):deffunc (self):Print "FUNCC"classD (b,c):Pass#instantiating an objectobj =D ()#new class function call, print FUNCCObj.func ()
example code for classic class and modern class method invocation order
Execution Result:
1>>>##经典类的多继承2...classA:3...deffunc (self):4...Print "Funca"5 ... 6>>>classB (A):7...Pass8 ... 9>>>classC (A):Ten...deffunc (self): One...Print "FUNCC" A ... ->>>classD (b,c): -...Pass the ... ->>>#instantiating an object -... obj =D () ->>>#function call, printing Funca + ... obj.func () - Funca +>>> A>>>##新式类的多继承 at...classA (object): -...deffunc (self): -...Print "Funca" - ... ->>>classB (A): -...Pass in ... ->>>classC (A): to...deffunc (self): +...Print "FUNCC" - ... the>>>classD (b,c): *...Pass $ ... Panax Notoginseng>>>#instantiating an object -... obj =D () the>>>#new class function call, print FUNCC + ... obj.func () AFuncc
#从以上结果可以看出
The search for the classic class is based on the principle of depth first, the order of inquiry: D->b->a, if not found, the error, found on the call
New type of query is the principle of breadth First, query order: d->b->c->a, if not found, the error, found on the call
The object-oriented thing.