This article is an example of Python's object-oriented thinking. Share to everyone for your reference. The specific analysis is as follows:
The basic idea of object-oriented is encapsulation, inheritance, polymorphism.
The first is inheritance:
Define a class:
Copy Code code as follows:
Class Bird (object):
Have_feather = True
way_of_reproduction = ' egg '
Call this class:
Copy Code code as follows:
Summer = Bird ()
Print Summer.way_of_reproduction
Unlike Java, Python does not need new to instantiate a class.
Similarly, the Python class is a way to set the method:
Copy Code code as follows:
Class Bird (object):
Have_feather = True
way_of_reproduction = ' egg '
Def say (self, word= ' Hi Hi '):
print ' I say: ' + word
Note that the 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.
Copy Code code as follows:
CHK = Chicken ()
Print Chk.have_feather
Print Chk.sat (' Hello ')
__init__ () method
__init__ () is a special approach (special method). There are special ways in Python that Python handles them in a special way. The name of a particular method is characterized by a two underline before and after.
The special of the __init__ () method is that if you define this method in a class, once you create an object from that class, Python automatically invokes the method (this process is also called initialization).
Such as:
Copy Code code 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:
Copy Code code 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 variable of the parent class
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 allows multiple inheritance, but this is a very risky operation and is not recommended for casual use.
With regard to Python polymorphism, like JavaScript, direct access to the object's properties requires no interface, no type conversions.
For the type of judgment, there are the type () functions of the grasping, and the isinstance () function to determine whether a subclass of a function.
Copy Code code as follows:
Isinstance (object, ClassInfo)
To determine whether an instance is this class or object is a variable
ClassInfo is a type (tuple,dict,int,float)
To determine whether a variable is this type
Copy Code code 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 results:
True
True
True
I hope this article will help you with your Python programming.