Python object-oriented programming one

Source: Internet
Author: User

First, class

Object-oriented is the classification and encapsulation of functions, its main purpose is to improve the repetition of the application of the program, make development more convenient and fast.

in our cognition, we will classify things according to similar attributes. For example: fish, the common property of fish is respiration, oviparous. Any fish is created on the basis of this fish.

definition of fish for human fishes:    

Class Fish:

Have_breath = True

reproduction = ' egg '

First define a category class, Fish fishes, in which there are two variables defined, one is a breathing have_breath, one is the promotion of reproduction, these two are fish properties.


Now I keep a goldfish, called goldfish. It is an actual object and belongs to fish, using the defined fish class:

Goldfish = Fish ()

Print Goldfish.reproduction

The first sentence is to create an object, stating that Goldfish is an object in fish, and goldfish has the properties of the fish, and the reference to the attribute is implemented by the form of an object.

Second, the method

There is only one special difference between a method of a class and a normal function -they must have an extra first parameter name , but you do not assign a value to this parameter when calling this method. Python will provide this value.

For example:

#定义鱼类

Class Fish:

#变量

Have_breath = True

reproduction = ' egg '

#新增方法, moving distance

def move (self, dx):

Position = DX

return position

Goldfish = Fish ()

print ' Move: ', Goldfish.move (10)


A new method attribute move is redefined, and the first parameter of the method is the self that must be written, regardless of the use of the fish.

When you call the Move method, only the DX parameter is passed.

Three main features of object-oriented

Inheritance, encapsulation, polymorphism

1. Inheritance

Can be simply understood as the child inherits the content of the parent, following the use of fish for example.

Fish are divided into goldfish, crocodiles and so on.

#金鱼类 (gdfish):

class Gdfish (fish):

Eat_person = False

Have_fin = True

#鳄鱼类 (Crocilisk):

Class Crocilisk (fish):

Eat_person = True

Have_fin = False

#定义金鱼对象, call the Gdfish class

Goldfish = Gdfish ()

#打印gdfish自身的属性

Print Goldfish.eat_person

#打印fish类父类的属性

Print Goldfish.have_breath

#调用fish类父类的方法

print ' Move: ', Goldfish.move (10)

To get:

True

False

Move:10

The above example is a new definition of two classes, gold fish gdfish and crocodile class Crocilisk, the increase of two properties, whether to eat people eat_person, whether there is a fish fin have_fin. In parentheses, the parent fish is represented. The gold fish gdfish and the alligator Crocilisk are the sub-classes of fish fishes, which inherit the fish's properties and methods, and can call and use the methods and properties of the parent fish directly. The above object goldfish, although only for the gold fish Gdfish was instantiated, but because the gold fish gdfish automatically inherit the fish's properties and methods, so, the object goldfish automatically have fish properties and methods.

With inheritance, you can reduce the duplication of information and duplicate statements in your program. If you define two categories of gold fish and crocodiles, instead of inheriting them, then you need to write the properties and methods of the fish separately into the definition of the gold fish and the crocodile class, so that the code is bloated and cumbersome, and inheritance features reduce the occurrence of such problems.


2. Encapsulation

Encapsulation, that is, to encapsulate the content somewhere, and then to call the North package somewhere in the content.

For example:

#定义鱼类

Class Fish:

def __init__ (SELF,DX)

self.dx = dx

Goldfish = Fish (10)

When the program executes, self is a formal parameter, goldfish = Fish (ten), Self equals goldfish

So, the content is encapsulated inside the goldfish. Goldfish objects also have DX moves belonging to. The following shows the contents of the call to its encapsulation.

(1), direct call via Object

Class Fish:

def __init__ (SELF,DX)

self.dx = dx

Goldfish = Fish (10)

#通过 goldfish directly calls the DX property.

Print GOLDFISH.DX

Returns: 10

(2), indirectly through self-invocation

Class Fish:

def __init__ (SELF,DX)

self.dx = dx

def move (self):

Print SELF.DX

Goldfish = Fish (10)

#程序会将goldfish传递给self参数, the encapsulated content is printed by calling the method of the fish class.

      Span style= "Font-size:16px;color:rgb (51,51,51); line-height:37.8378px;font-family: ' The song Body ', Simsun;background-color: RGB (255,255,255); " >goldfish.move ()  

Returns: 10

3. polymorphic

    polymorphic Meanings, literally understand a variety of forms of expression. It is implemented in the class's inheritance and is reflected in the method invocation of the class. Polymorphism means that the variable does not know what the referenced object is, and behaves differently depending on the object being referenced. The following example illustrates the meaning of polymorphism.

For example:

The Sounds of Animals

Cat sounds: Meow meow ...

Shout: Bark ...

Wolf cry: Whining ...

Code:

#定义动物类

Class Animal:

def __init__ (self,name):

Self.name = Name

#定义cat类, inherit the property of animal name

Class Cat (Animal):

def shout (self):

Return ' Miaomiao '

#定义dog类, inherit the property of animal name

Class Dog (animal):

def shout (self):

Return ' Wangwang '

#定义wolf类, inherit the property of animal name

Class Wolf (animal):

def shout (self):

Return ' Wangwang '

#定义对象cat1, for Cat class instantiation

CAT1 = Cat (' Zhenni ')

#调用animal的类的属性name和cat1的自己的方法shout

Print Cat1.name + ': ' + cat1.shout ()

As can be seen from the above example, the bark of the animals, cats, dogs, wolves are not the same, each has its own characteristics, but for the parent class animal, each subclass inherits the parent class property name, but each has its own different characteristics, this shows that the sub-class show the characteristics of polymorphism.


Python object-oriented programming one

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.