Basic Python Basics 08 Object-oriented concepts

Source: Internet
Author: User

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 based on

Object, understanding OOP is the key to further learning 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 categories, for example, birds ' common properties are feathers, through spawning

To produce offspring, any particular bird is based on the bird's archetype.

Object-oriented is the simulation of the above human cognitive processes, in the Python language, we call objects (object)

Defining birds

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 belong to this analogy, we define two variables,

One is feather (have_feather), one is reproductive (way_of_reproduction), these two variables correspond to what we just said

Property (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, he was an object and belonged to birds, using the classes defined earlier:

Summer = Bird ()

Print Summer.way_of_reproduction

Create the object by the first sentence, and explain that summer is an object in the category bird, Summer has the bird's Class property, and the reference to the property is passed

An object that is implemented in the form of an Object.attribute property.

Action

In everyday cognition, when we identify categories through attributes, we sometimes distinguish between categories based on what this thing can do, for example, birds can fly,

The internal definition function of a class in Python solves the problem

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.

There is a self in its argument, which is to make it easier for us to refer to the object itself. Method with a parameter must be self whether or not it is used, about self

The content will be shown in the next lecture

For the other two parameters, dx dy represents the distance traveled in 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)

Sub-class

The column itself can be further subdivided into sub-classes

For example, birds can be further divided into chicken geese oriole

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

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)

The newly defined chicken (Chicken) class, which adds two properties: Move mode (Way_of_move), may be found in KFC (

POSSIBLE_IN_KFC)

In the class definition, the parentheses are Bird, which shows that chicken is a subclass of the bird (Bird), that is, chicken inherits from Bird.

Naturally, Bird is the parent of ckicken. Chicken will enjoy all the properties of bird. Even though I'm just stating 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 birds, we must

By inputting birds into the definition of chicken and oriole, the process becomes tedious, so object-oriented improves the repeatable usability of the program.

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

Classifying all sorts of things to understand the world, starting with the human ancestor, we are practicing this authentication process, object-oriented is

The habit of human thinking. The so-called process-oriented, that is, the execution of playing a statement in 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

The parent class-subclass relationship is described with inheritance, and the subclass automatically has all the properties of the parent class.

Self represents an object created from a class definition.

Create an object: Object name = Class name ()

Properties of the referencing object: Object.attribute

Python basic 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.