Basic python tutorials-some object-oriented concepts, basic python tutorials

Source: Internet
Author: User

Basic python tutorials-some object-oriented concepts, basic python tutorials

Python uses classes and objects to program object-oriented (OOP.

The main purpose of object-oriented is to improve the reuse of programs. 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 the key to further learning Python.

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

Similar object, classified as Class

In human cognition, things are classified according to similar attributes and named for categories. For example, birds have feathers and generate offspring through spawning. Any particular bird is built on the prototype of the bird.

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
Copy codeThe Code is as follows:
Class Bird (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 their content. It is recorded as Question 1.

Suppose I have a chicken named summer. It is an object and belongs to birds. Use the class defined earlier:
Copy codeThe Code is as follows:
Summer = Bird ()
Print summer. 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, and the reference to the attribute is through the object. attribute (object. attribute.

Poor summer, you are a hairy egg.

Action

In our daily cognition, when we identify a category through attributes, we sometimes differentiate categories based on what this thing can do. For example, birds move. In this way, the bird is separated from the category of the house. These actions will lead to certain results, such as location changes caused by moving.

Some of these "behavior" attributes are method ). In Python, you can define functions in the class to describe the method.

Copy codeThe Code is as follows:
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 to indicate the move method. (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 reference 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.

Subclass

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 express the above concepts through inheritance.

Copy codeThe Code is as follows:
Class Chicken (Bird ):
Way_of_move = 'walk'
Possible_in_KFC = True

Class Oriole (Bird ):
Way_of_move = 'fly'
Possible_in_KFC = False

Summer = Chicken ()
Print summer. have_feather
Print summer. move (5, 8)

For the newly defined Chicken class, two attributes are added: the moving method (way_of_move), which may be found in KFC (possible_in_KFC)

In class definition, Bird is enclosed in brackets. This indicates that Chicken is a child of Bird, that is, Chicken inherits from Bird. Naturally, Bird is the parent class of Chicken. Chicken will have all the attributes of Bird. Although I only declared that summer is a chicken class, it inherits the attributes of the parent class (whether it is the variable property have_feather or the method property move)

The newly defined Oriole class also inherits from birds. When creating a porn object, the object automatically has the attributes of birds.

Through the inheritance system, we can reduce repeated information and statements in the program. If we define two classes separately, instead of inheriting from birds, we must input the attributes of birds into the definition of the Rooster and the yellow catalogue respectively. The entire process becomes cumbersome. Therefore, the object-oriented method improves the reusability of the program.

(Return to Question 1. The object in the brackets. If it is an object in the brackets, it indicates that this class has no parent class (to the beginning ))

We classify all kinds of things to understand the world. Starting from the human ancestor, we are practicing this cognitive process. Object-oriented is in line with human thinking habits. The process-oriented approach is to execute a statement and then execute the next one. It is more about machine thinking. Through object-oriented programming, we can easily express complicated ideas in our thinking.

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 object created according to the class definition.

Create an object: Object Name = Class Name ()

Attribute of the referenced object: object. attribute


Python object-oriented Basics

Class Test:
Def setdata (self, value ):
Self. data = value
Def display (self ):
Print self. data

X = Test ()
Y = Test ()

X. setdata ("test ")
Y. setdata (123456)

X. display ()
Y. display ()
======================================
X. setdata = "test"
Y. setdata = 123456
Change
X. setdata ("test ")
Y. setdata (123456)
That's all.
The reason is that the Test object does not have the setdata attribute and only has the setdata method. The method is called in the form of setdata.

Hope to help you!

Python object-oriented

The second is equivalent to a local variable. Except for the init function, it cannot be accessed anywhere else.
The third type is equivalent to the class property. Each specific object has a different value. In other classes, if object B is generated, such as B = a (), B is used. aa can be accessed.
The first is to define the class variable. All objects share the variable and use. aa can be accessed. Note that Class a is the name of the class or can be accessed through each object. For example, B = a () c = a (), B. aa and c. aa and. aa is an access item with the same value.

Related Article

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.