Python object-oriented ideology analysis and python object-oriented Ideology
This article describes Python's Object-oriented thinking. Share it with you for your reference. The specific analysis is as follows:
The basic idea of object-oriented is encapsulation, inheritance, and polymorphism.
The first is inheritance:
Define a class:
Copy codeThe Code is as follows: class Bird (object ):
Have_feather = True
Way_of_reproduction = 'egg'
Call this class:
Copy codeThe Code is as follows: summer = Bird ()
Print summer. way_of_reproduction
Unlike Java, Python does not need new to instantiate classes.
Similarly, Python classes can be set as follows:
Copy codeThe Code is as follows: class Bird (object ):
Have_feather = True
Way_of_reproduction = 'egg'
Def say (self, word = 'hihi '):
Print 'I say:' + word
Note that all class functions must have at least one parameter, which must be self.
Functions other than classes do not have this restriction.
Copy codeThe Code is as follows: chk = Chicken ()
Print chk. have_feather
Print chk. sat ('hello ')
_ Init _ () method
_ Init _ () is a special method (special method ). Python has some special methods. Python will process them in a special way. The name of a special method is characterized by two underscores.
The special feature of the _ init _ () method is that if you define this method in the class, once you create an object based on this class, python will automatically call this method (this process is also called initialization ).
For example:
Copy codeThe Code is as follows: class happyBird (Bird ):
Def _ init _ (self, more_words ):
Print 'we are happy birds. ', more_words
Hb = happyBird ('Happy, Happy! ')
Overload of the parent method:
Copy codeThe Code is as follows: class Hello (object ):
Name = 'hello'
Def _ init _ (self ):
Self. name = 'My name is hello'
# The parameters in the class must contain the self Parameter
Def sayhi (self ):
Print 'Hi you'
Class World (Hello ):
Def _ init _ (self ):
# The variable name initialized by the parent class is accessed here.
Print 'before: ', Hello. name
Super (World, self). _ init __()
# Because the initialization constructor of the parent class is called, the variable changes of the parent class are inherited.
Print 'after: ', self. name
# Similar to method overload
Def sayhi (self, word = 'baby '):
# Call the sayhi method of the parent class
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 multi-inheritance, but this is a very dangerous operation. We recommend that you do not use it at will.
For Python polymorphism, just like JavaScript, you can directly access object attributes without using interfaces or type conversion.
For the type judgment, we can refer to the type () function and the isinstance () function to determine whether a function is a subclass.
Copy codeThe Code is as follows: isinstance (object, classinfo)
Determines whether the instance is a class or an object is a variable.
Classinfo is a type (tuple, dict, int, float)
Determines whether the variable is of this type.
Copy codeThe 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
I hope this article will help you with Python programming.