Python object-oriented chapter felt self

Python object-oriented programming Thoughts

Have learned object-objects are objects that exist in the objective world, the whole things in the world can be an object. He says generalization, object-oriented technology is a way to simulate the objective world from the organizational structure. On my subjective experience and not well understood this programming ideas, there is no clear difference between it and feel the process-oriented programming, perhaps because I was in contact with programming for six months, in a subsequent study, I will continue to explore .
1. Overview of Object Oriented
(1) target
objects are divided into static and dynamic parts and static parts are called "attributes" refers to the property of the object itself, such as gender, height, perpetrator dynamic portion of an object, such as a human can walk ,Run.
A class of physical properties and behavior of the same type is called, it is a carrier class attributes and behavior of the encapsulated object. I understand that after learning in class is extremely important, I do not know other languages is not a probability but also the class ......
object-oriented programming has encapsulation, inheritance and polymorphism three basic characteristics. Differences are as follows:

feature concept effect
Package It refers to the class as a carrier attributes and behavior of objects encapsulate Thinking is the core of object-oriented programming, to ensure the integrity of the internal data of the class
inherit Subclass multiplexing properties and behavior of the parent class while there are specific attributes and behavior It is an important means to achieve reuse
Polymorphism The parent class object to the feature subclass Subclass inherits the parent class warrior, also have their own characteristics and effects can be achieved without

In a subsequent study, I have a preliminary understanding of encapsulation, inheritance, and polymorphism of the meaning and effect, very magical ......
2. Class definition and use of
the definition of the class need to use the keyword class (Why is class?), Syntax class className: , followed by the application syntax name = classname () creates an instance of the class.
After you create a Python class, you can create _ the init () method, the book says I studied similar to Java constructor, the constructor will call it. This constructor creates the first parameter must self, he is accessing properties and methods used in class and automatically passed arguments, thus creating the instance does not need to pass parameters to the self. But why should define a constructor? This is where I am confused. I learned this method is to make examples of binding properties.
Examples of members of the class data members and methods. Examples of the method is simply a function defined in the class, but the first argument must be a self. Data member that is simply a variable defined in the class, it is divided into classes and instance properties, property class means a class variable defined outside the function, instance variables defined attribute refers to a function in the class.
Finally, there is the class limit access, it can be considered personally think that is evidenced by the ability of the package. We can attribute added before or single underlined method name
, such as the _foo; __ double underlined, as __foo; ending or double underlined, as __foo__.

Types of It indicates the type of effect
Single underline It represents the type of protection Allowing only the class itself and its subclasses access
Double underline It represents the private type Only the definition of the class itself access method
Inclusive double underline Means that the definition of special methods unknown

3. Properties
In addition to the above properties, we can @propetry by the method is converted into a property. This property can be used to calculate such and can be accessed directly by the process of the method name. Format is as follows:

//@property
    def name(self):
        body

Through this method we can also attribute add a security mechanism, that is private property to achieve access restrictions.
4. inheritance
object-oriented programming, the inherited class or a base class is called the parent, the new class is called the child class or a derived class, inherited through the following format:

//class name(基类):
    body

When using inheritance, if the base class is a subclass method does not apply, we need to rewrite this method until our demands. Not automatically call the base class constructor in the subclass, it is necessary to use super () calls the base class constructor function in the subclass.
** Summary: roughly learning the Python object-oriented programming section, thinking of object-oriented programming is still not clear enough. For some functions or class definitions, and no description is given on a book, grasp a whole are insufficient. Proposed multi actual combat, deepen understanding and application of knowledge. Finally, this chapter gives me the feeling is magical and mysterious.

Guess you like

Origin www.cnblogs.com/trainking-star/p/12232587.html