Python object-oriented programming, python object-oriented
In the previous chapter, I learned a lot about python-socket programming by reading books. One important point is that I don't know what the python class is. What is the meaning of every piece of code in Baidu, it seems that the problem is quite big. What should I do if the class still appears in the future ???, Decisive Baidu "python Object-Oriented Programming", the following are some experiences in learning python object-oriented programming,Original article from Wu peiqiAnd changed it in many places.
Outline
1. Create classes and objects
2. Three features of object-oriented
Create classes and objects
Object-Oriented programming is a programming method, which must be implemented using classes and objects. Therefore, object-Oriented Programming is actually the use of "class" and "object.
A class is a template. The template can contain multiple functions, and some functions are implemented in the function.
The object is an instance created based on the template. You can use the instance object to execute functions in the class.
I can see such a sentence from zhihu. I don't know if it's right or wrong. I can't understand it, and I feel too advanced.
"The instance means that the object opens up space in the memory.
The girl friend (object) in the parents' mouth is holding her (instance) in his arms )."
Create a class:
# Create class Foo:
# The function defined in the class is called a method.Def Bar (self ):# The first parameter of a function in the class must be self.Print ("bar") def Hello (self, name): print ("I am % s" % name) # create object objobj = Foo () based on class Foo () # It is equivalent to getting all the methods (functions) in the class. Of course, this is what I have to understand # Foo (). bar () obj. bar () # execute the Bar method obj. hello ("smelond") # execute the Hello Method
# Object-oriented: [object creation] [object execution method]
# Function programming: [execution function]
Three features of object-oriented
I. Encapsulation
Encapsulation, as its name implies, encapsulates the content to a certain place and then calls the content encapsulated in a certain place.
Therefore, when using the object-oriented encapsulation feature, you need:
- Encapsulate content somewhere
- Call encapsulated content from somewhere
Step 1: encapsulate content somewhere
# Create class Foo: def _ init _ (self, name, age): # It is called a constructor. self is automatically executed when an object is created based on the class. name = name self. age = age # create an object based on the class Foo # automatically execute the _ init _ method of the Foo class obj1 = Foo ("wupeiqi", 18) # encapsulate wupeiqi and 16 to (obj1 AND self) respectively) in the name and age attributes of # create an object based on class Foo # automatically execute the _ init _ method obj2 = Foo ("smelond", 16) of the Foo class) # encapsulate smelond and 16 in the name and age attributes of (obj2 AND self) respectively.
Self is a form parameter. When obj1 = Foo ('wupeiqi ', 18) is executed, self is equal to obj1
When obj2 = Foo ('Alex ', 78) is executed, self is equal to obj2
Therefore, the content is encapsulated into the objects obj1 and obj2. Each object has the name and age attributes, which are saved in memory.
Step 2: Call encapsulated content from somewhere
When calling encapsulated content, there are two situations:
- Direct call through objects
- Indirect call through self
1. directly call encapsulated content through an object
Call method: object. property name
The preceding Code directly calls the encapsulated content print (obj1.name) print (obj1.age) print (obj2.name) print (obj2.age) to output wupeiqi18smelond16
2. Indirectly calling encapsulated content through self
# Indirectly calling class Foo: def _ init _ (self, name, age): self. name = name self. age = age def detail (self): print (self. name) print (self. age) obj1 = Foo ('wupeiqi ', 18) obj1.detail () # Python will pass obj1 to the self parameter by default, that is, obj1.detail (obj1). Therefore, in this case, the self = obj1 in the method is self. the name is wupeiqi; self. age is 18obj2 = Foo ('smelond ', 16) obj2.detail () # Python will pass obj2 to the self parameter by default, that is, obj2.detail (obj2). Therefore, in this case, the self = obj2 in the method is self. the name is smelond; self. age is 17 output: wupeiqi18smelond16
To sum up, for object-oriented encapsulation, the constructor is used to encapsulate the content into the object, and then the encapsulated content is obtained directly or indirectly through the object or self.