Basic Python Basics 08 Object-oriented concepts

Source: Internet
Author: User

Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!

Thanks upstream, Topmad and liqing correction

Python uses classes (class) and objects (object) to program object-oriented (object-oriented programming, or OOP, for short).

The main purpose of object-oriented is to improve the reuse of the program. The reason we cut into object-oriented programming so early is that the whole concept of Python is Object-based. Understanding OOP is the key to further learning about Python.

The following is an object-oriented understanding, based on classification.

Similar objects, grouped into classes

In human cognition, things are categorized according to their attributes and named for the category. The common property of birds, for example, is feathers, which produce offspring by spawning. Any particular bird is based on the bird's prototype.

Object-oriented is the simulation of the above human cognitive process. In the Python language, in order to sound cool, we call the "things" mentioned above as objects (object).

Define birds first

Class Bird (object):    have_feather = True    way_of_reproduction  = ' egg '

We define a category (class), which is the bird (Bird). In the block of statements that are part of this analogy, we define two variables, one with feathers (have_feather) and one reproductive (way_of_reproduction), which correspond to the attributes we just said (attribute). For the time being, we do not state the brackets and the contents of the parentheses, which are recorded in question 1.

Suppose I had a chicken, called summer. It is an object and belongs to the birds. Use the classes defined earlier:

Summer = Bird () print summer.way_of_reproduction

The object is created by the first sentence, and the summer is an object in the category bird, and summer has the bird's Class property, and the reference to the property is implemented in the form of an object. Property (Object.attribute).

Poor summer, you are a stuffed egg, fine.

Action

In everyday cognition, when we identify categories through attributes, we sometimes distinguish categories based on what can be done by this thing. For example, a bird will move. In this way, the bird is separated from the category of the house. These actions can lead to certain results, such as movement leading to changes in position.

Some of these "behavior" properties are methods. The method is described in Python by defining functions inside the class.

Class Bird (object):
Have_feather = True
way_of_reproduction = ' egg '
def move (self, dx, dy): position = [0,0]
Position[0] = position[0] + dx position[1] = position[1] + dy
return position

Summer = Bird () print ' After move: ', Summer.move (5,8)

We redefined the bird category. The bird adds a method attribute, which is the Move method. (I admit that this method is silly, you can define an interesting method after you have read the next one)

(It has a self in its arguments, which is to make it easier for us to refer to the object itself.) The first argument of a method must be self, whether or not it is used. The contents of self will be expanded in the next lecture)

The other two parameters, dx, dy, represent the distance traveled in the X, y two directions. The Move method eventually returns the position that have been calculated.

At the end of the call to the Move method, we passed only the DX and dy two parameters, and do not need to pass the self parameter (because self is for internal use only).

My summer can run.

Sub-class

The category itself can be further subdivided into sub-classes

For example, birds can be further divided into chickens, geese, oriole.

In OOP, we express these concepts through inheritance (inheritance).

Class Chicken (Bird):    way_of_move = ' walk '    POSSIBLE_IN_KFC = Trueclass Oriole (Bird):    way_of_move = ' Fly '    POSSIBLE_IN_KFC = Falsesummer = Chicken () print Summer.have_featherprint summer.move (5,8)

Newly defined chicken (Chicken) class, added two properties: Move way (Way_of_move), possibly found in KFC (POSSIBLE_IN_KFC)

In the class definition, in parentheses for bird. This shows that chicken is a subclass of birds (Bird), that is, chicken inherits from Bird. Naturally, Bird is the parent of chicken. Chicken will enjoy all the properties of bird. Although I only declare that summer is a chicken, it inherits the attributes of the parent class (either the variable attribute Have_feather or the method property move)

The newly defined Oriole (Oriole) class, likewise inherited from birds. When you create a Oriole object, the object automatically owns the bird's properties.

Through the inheritance system, we can reduce the duplication of information and duplicate statements in the program. If we define two classes separately and do not inherit from the birds, we must enter the birds ' attributes into the definitions of the chickens and the Oriole classes separately. The whole process becomes tedious, so object-oriented improves the reuse of the program.

(Back to question 1, object in parentheses, when object in parentheses indicates that the class does not have a parent class (header))

By classifying all sorts of things to understand the world, starting with the human ancestor, we are practicing this cognitive process, and object-oriented is in accordance with the habits of human thinking. The so-called process, that is, executing a statement and then executing the next, more machine thinking. Through object-oriented programming, we can more easily express the complex ideas in the thinking.

Summarize

Classify things according to attributes (classify object as Class)

Method is a property that represents an action

Use inheritance to illustrate parent class-subclass relationships. Subclasses automatically have all the properties of the parent class.

Self represents an object created from a class definition.

Establish an object: Object name = Class name ()

Properties of the referencing object: Object.attribute

Basic Python Basics 08 Object-oriented concepts

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.