Basic concepts of Python 08 object-oriented

Source: Internet
Author: User

Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!

Thank you,Topmad and Liqing Error Correction

 

(Object-Oriented is not difficult. Don't be scared away by "object-oriented)

In python, classes and objects are used for implementation.Object-oriented(Object-Oriented Programming (OOP)Programming.

The main purpose of object-oriented programming is to improveProgramOfReusabilityThis is similar to the purpose of a function.

The reason for getting started with object-oriented programming so early is that the entire concept of python is based on objects. Understanding OOP is essential for us to have a deep understanding of Python.

The following is my understanding of object-oriented.

 

1.Class is the classification of objects with similar properties.

In human cognitionSimilar PropertiesPut thingsCategoryAnd name the category. For example, birds have feathers and generate offspring through spawning. Any special bird is in the bird'sPrototype.

Object-oriented means simulating the above human cognitive processes. In python, to sound cool, we call the "things" mentioned above as an object ).

Define birds first

 
ClassBird (object): have_feather=True way_of_reproduction='Egg'

We define a class, that is, Bird ). In the block of this analogy, we define two variables: have_feather and way_of_reproduction ), these two variables correspond to the attribute we just mentioned ). For the moment, we do not describe the brackets and the content in them.Question 1.

Suppose I have a chicken named Summer. It is an object belonging to birds. Use the class defined above.

Summer =Bird ()PrintSummer. way_of_reproduction

Create an object in the first sentence and describe that the summer is an object in the category bird. The summer has the class attribute of the bird.Object. Attribute).

(Poor Summer, you are an egg. It is very delicate)

 

2.The attribute can be a variable or an action (method).

In human daily cognition, when we identify a category through attributesWhat can I do?To distinguish categories. For example, the bird moves (this is separated from the category of the House ). These actions will bring about some results, and moving will bring about location changes.

For the sake of cool, we call these attributesMethod). In python, you can define functions in the class to describe the method.

ClassBird (object ):
Have_feather=True
Way_of_reproduction='Egg'
DefMove (self, dx, Dy): Position = [0, 0]
Position [0]= Position [0] +DX position [1] = position [1] +Dy
ReturnPosition

Summer = bird ()Print 'After move:', Summer. Move (5, 8)

We redefined the bird category.

The bird adds a method attribute, that is, moving ). (I admit that this method is silly. You can define an interesting method after reading the next lecture)

(Its parameters include self, which is used to facilitate reference of the object itself.The first parameter of the method must be self, whether or not it is used.The content about self will be expanded in the next lecture)

The other two parameters, Dx and Dy, indicate the distance between x and y. The move method returns the position after the operation.

When we finally call the move method, we only pass the Dx and Dy parameters, and do not need to pass the self parameter (because self is only used internally ).

(My summer can run now)

 

3.The category itself can be further subdivided into sub-classes.

For example, birds can be further divided into chickens, geese, and yellow rays.

In Oop, we useInheritance)To express the above concepts.

ClassChicken (BIRD): way_of_move='Walk 'possible_in_kfc=TrueClassOriole (BIRD): way_of_move='Fly'Possible_in_kfc=Falsesummer=Chicken ()PrintSummer. have_featherPrintSummer. Move (5, 8)

The newly defined chicken class adds two attributes, the moving method (way_of_move) and the possible finding in KFC (possible_in_kfc)

In class definition, change the brackets to bird to indicate that chicken belongs to a bird.Subclass(For cool, chicken inherits from bird.) Naturally, bird is the chickenParent class. Through this instruction, python will know,Chicken has all the attributes of bird. We can see that although I only declared that summer is a chicken class, it still has the attributes of birds (whether it is the variable attribute have_feather or the method attribute move)

In addition, it defines the oriole class, which also inherits from birds. In this way, when we have an object that belongs to the yellow dragonfly, we will also automatically own the attributes of birds.

Through the inheritance system, we can avoid repeated information and statements in the program. If we define two classes instead of inheriting from birds, we have to break the attributes of birds into the definition of chicken and porn respectively.

(ReturnQuestion 1, The object in brackets.ObjectIt indicates that this class has no parent class (to the beginning ))

So,Object-oriented improves the reusability of Programs.

We can see that,Object-oriented actually classifies various things based on the habits of human cognition.To understand the world. We may have been practicing this cognitive process for millions of years from the beginning, so object-oriented programming is very consistent with human habits. The so-called process-oriented (that is, executing a statement and then executing the next statement) is actually machine thinking. Through object-oriented programming, we are actually closer to our natural way of thinking, and more convenient to communicate with others about the ideas included in our program, even if that person is not a programmer.

 

Summary:

Classify things by attributes (classify objects as Class)

A method is an attribute that indicates an action.

Use inheritance to describe the parent class-subclass relationship. Subclass automatically has all attributes of the parent class.

Self represents the objects created based on the definition of this class.

Definition class:

 
ClassClass_name (parent_class):=... B=...DefMethod1 ():...DefMethod2 ():...

Create an object: Object Name = Class Name ()

Attribute of the referenced object: object. Attribute

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.