Python's self-understanding of object-oriented development

Source: Internet
Author: User

? Detailed code understanding can be consulted

Stupid Bird Tutorial Blog: http://www.runoob.com/python3/python3-class.html

Object-oriented is often mentioned, so what is object-oriented?

Its basic concepts: classes, objects, instances, attributes, methods, and encapsulation, inheritance, polymorphism, and what are the combinations?

objects can be things in the world.

Process oriented: It is the process of using data as the center to process data in accordance with human logic, such as C language code.

For example, putting an elephant in the refrigerator is a total of three steps:

The first step: Open the refrigerator door;

Step two: Put the elephant in the refrigerator;

Part Three: Close the refrigerator door.

It can be seen that the underlying implication of this logic is that these things have been done, you or me or xxx to open the refrigerator door, to put the elephant, to close the refrigerator door.

Object-oriented: The main line is not to complete the event step by person, but to the world (object, and is a dynamic object, in particular, contains the data characteristics that is the attribute and the ability is the method (or function)) as the main line, and then assemble the object properties and methods to complete the task.

Or put an elephant in the fridge.

In this case, the refrigerator can be used as the object, the refrigerator has the door closed data (can be 0 for closing, 1 for open door) is the property. The ability to open and close doors can be a method (a method is also called a function). So the elephant into the refrigerator is not a person to open the refrigerator, the refrigerator is equivalent to the automatic refrigerator, will open the door. People can also be seen as objects, there are ways to put elephants into the refrigerator.

The above refrigerators and people are objects, then what is an example?

The object is a collection of abstract generalizations, like the concept of a horse, you cannot find a black and white fat and thin horse, but you can find the white champ, the black champ, but these horses belong to the above concept horse. So an object is an abstract concept, and an instance is a materialized object. If the refrigerator object, then his example can be Haier refrigerator, can be gree refrigerator (do not know gree has no refrigerator) and so on.

What is the attribute:

properties are actually data. The method is actually a function. The class is the abstract object.

The properties of an object exist inside the class, but outside the method of the class.

The properties of an instance are placed inside a class inside a method or under an instance outside the class. Divided into private and non-private properties. Private instance properties begin with a double underscore, can only be defined within a class, cannot be accessed outside of the class, even if you use an instance. __ Private properties are accessed only by creating an instance property outside of the class. Because once a private property in a class is established, Python renames the Private instance attribute (which, of course, cannot be established outside the class) in the background. So you are unable to access instance properties within the class in the instance. Where self represents the meaning of the instance.

1 #This is the comment line2 #The following is an in-class3 classClass 1:4Class Property 1=1#Class properties are defined within a class, outside the class method5Class Property 2=26     __ Class Property 3="Tom" #7     def __init__(SELF,A1,B1):#A, B is a parameter passed in instance 1, this is a special class method, called the constructor, once this class 1 is created an instance will automatically call this class construction method in the background. 8Self. Instance Properties 1=a1#defines an instance property9Self.__ Instance Properties 2=b1#A double underscore begins with a private instance property that is not accessible outside the class, and Python will rename the private instance property in the background after it is defined.Ten     defMethod 1 (self): OneSelf. Instance Properties 1=1 ASelf. Instance Properties 2=2 -           -     defMethod 2 (self): theSelf. Instance Properties 1=1 -Self. Instance Properties 2=2 -          Print('and for', self. Instance properties 1+self. Instance Properties 2)#using instance properties within a class -     Print('Class attribute 1 for Class 1:', Class 1. class attribute 1,'Class attribute 2 for Class 1:', Class 1. Class attribute 2,)#Class properties are used within classes, regardless of whether they are inside or outside the class, using class attributes is the class name. Class Property Name +  - #The following is an out-of-class + if __name__=='__main__': AExample 1 = Class 1 (A, B)#The class is equivalent to a mold, which has all the non-private properties and methods of the class.  at     Print('Example 1. Instance Properties 1')#The instance property is used outside the class, this instance property 1 is the Instance property 1 in the constructor, and the instance properties in Method 1 and Method 2 are not yet available because these methods are not yet called, and the instances under all those methods are -     #Is not available, but the constructor is automatically called when an instance is created by the class -Class 1. Class attribute 2=4#You can modify the class properties outside of the class, and all instances inherit such property values from all of them.  -Example 1.__ Instance Properties 2=3#in this attempt to change the private instance properties, the private instance properties cannot be accessed outside the class, so a new out-of-class instance attribute that is equivalent to a non-unspoken rule is not given an error, but is generally not done -Example 1. Instance Properties 5=5#It is possible to use this as an instance property created under an instance. -Example 1. Method 2 ()#this is called the class method outside the class, self is the default shape parameter, the argument can not, so the method 2 parameter is not written. 

I am afraid you can not see clearly, the following map to serve:

Objects exist in the problem analysis phase, and the object defined when it is converted to code is called a class.

Python's self-understanding of object-oriented development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.