Fortunately Python class with Australia 8 platform to build method of privatization

1. Create your own class

Australia 8 lucky buns source platform to build [Forum wowotoubbs.com] Q: 2171793408

Learning object-oriented first step is to create a class. Because the class is object-oriented foundation. Python classes and other programming languages ​​(Java, C #, etc.) of similar class, but also need to use the class keyword. By following a practical example look at how Python class is created.

This creates a regular class, and use this class to create two objects, and a method in which a call.

 

Result of the program as shown in FIG.


From the above code, we can understand the following knowledge points Python class.

Python class is defined using the class keyword, class name directly behind the class keyword followed.

Class is a block of code, so the back to the class name followed by a colon (:).

The method of the class is actually a function, defined by exactly the same method, but since the functions defined within the class, so in order to distinguish the function defined in the interior of the class called method.

We can see that the first parameter is the self of each method, in fact, this is a must. This is not necessarily called self parameter name (abc can call or any other name), but any of the method must specify at least one self argument, if the method contains a number of parameters, the first parameter as a self parameter. When calling the method, the values ​​of the parameters do not need to pass, the system will be the object method belongs pass this parameter. This method may be utilized within the parameters of the calling object resource itself, such as properties, methods, and the like.

variable name added by the self parameter is a property of the Person class can be accessed externally. The present embodiment sets the value of the name attribute person2 object, exactly the same method call person2.setName effect.

Creating an object using the class way calling function the same way. In the Python language, like Java does not need to use the new keyword to create an object, just use the class name with constructor (in later chapters will detail) parameter value.

Calling the object method, there are two ways, one method is to call directly through the object variable, the other is the method by calling the class object and the corresponding first parameter passing method. Person.greet used in this example (person2) to invoke a greet method person2 object.

If you use an integrated development environment, such as PyDev, PyCharm, then the code editor will have a good support for the object-oriented, for example, when you enter a point in the object variable (.), IDE will be listed in the object for us All resources that can be invoked, including methods and properties, as shown below.
2. Methods and privatization

Python class by default, all methods can be accessed externally. But like many other programming languages, such as Java, C #, etc., have provided private key to the method of privatization, that is internal methods can access only class method of privatization, privatization through the normal way is inaccessible objects method (unless you use a reflective technology, which is another matter). But did not provide private or similar keywords in Python class in the method of privatization, but Quxianjiuguo.

In the method name preceded by a double underscore Python class (__) allows this method is not accessible externally.

Guess you like

Origin www.cnblogs.com/renshengzhong/p/10974555.html