Object-oriented is one of the most mainstream ideas in programming languages at present. Python has a very good implementation for object-oriented. Also, with Python's flexible syntax, you can implement some cool object-oriented features.
Python has been an object-oriented language since its inception, which is why it is easy to create a class and object in Python. In this section we will detail Python's object-oriented programming.
If you have not been exposed to object-oriented programming languages before, you may need to first understand some of the basic features of object-oriented languages and form a basic object-oriented concept in your mind, which will help you learn more about Python's object-oriented programming.
Let's start with a brief look at some of the basic features of object-oriented.
Object-oriented difficult to learn is not the concept of knowledge points, but the object-oriented way of thinking:
The basic idea of object-oriented is encapsulation, inheritance, polymorphism.
The first is inheritance:
Define a class:
The code is as follows:
Class Bird (object): have_feather = True way_of_reproduction = ' egg '
Call this class:
The code is as follows:
Summer = Bird () print summer.way_of_reproduction
Unlike Java, Python does not require new to instantiate a class.
Similarly, the Python class below is a way to fix the method:
The code is as follows:
Class Bird (object): have_feather = True way_of_reproduction = ' egg ' def say (self, word= ' Hi Hi '): print ' I say: ' + word
Note that functions of all classes must have at least one argument, and this argument must be self.
A function other than a class does not have this restriction.
The code is as follows:
CHK = Chicken () print chk.have_feather print chk.sat (' Hello ')
__init__ () method
__init__ () is a special approach (special method). Python will have some special methods, and Python will handle them in a special way. The name of the special method is characterized by a two underscore before and after.
The special of the __init__ () method is that if you define this method in a class, Python automatically calls this method (which is also called initialization) once you have created the object from that class.
Such as:
The code is as follows:
Class Happybird (Bird): def __init__ (self,more_words): print ' We are happy birds. ', more_words HB = Happybird (' happy,happy! ')
Overloads of the Parent class method:
The code is as follows:
Class Hello (object): name = ' Hello ' def __init__ (self): self.name= ' My name is Hello ' #类中的参数必须带有self参数 def sayhi (self): print ' Hi you ' class World (Hello): def __init__ (self): #这里访问的是父类初始化的变量名 print ' Before: ', Hello.name super (world,self). __init__ () #由于调用了父类的初始化构造函数, inherits the change of the parent class's variable print ' after : ', Self.name #近似于方法重载 def sayhi (self,word= ' baby '): #调用父类sayhi方法 Super (world,self). Sayhi () print ' Hi ' +word def Sayworld (self): print ' Hi,hello world ' if __name__ = = ' __main__ ': c = World () c.sayhi () C.sayworld ()
In addition, Python is allowed to inherit multiple, but this is a very dangerous operation, it is recommended not to use it casually.
With regard to Python polymorphism, like JavaScript, direct access to the properties of an object without the need for an interface, no type conversion.
For the type of judgment, there are the type () function of the grab, and the isinstance () function determines whether the subclass of a function.
Copy the code code as follows:
Isinstance (object, ClassInfo)
Determine if the instance is this class or object is a variable
ClassInfo is a type (tuple,dict,int,float)
Determine if the variable is of this type
The code is as follows:
Class Obja: pass A = Obja () B = ' A ', ' V ' C = ' A string ' print isinstance (A, obja) print isinstance (B, tuple) Print isinstance (C, basestring)
Output Result:
True
True
True