python 12 off property (attribute) method (method)

Property (attribute) method (method)

 

Examples of the method in the first place must have self parameter

 

 

 

Class is created and calls

Create a class: class 

Class attributes created: assignment statement attribute = ...

Create an instance method: def method (self):

Examples of the class: class instance name = name ()

Call the class attributes: instance name attribute.

Method calls the class: instance name method ().

Examples of methods to create statements, and statements defined function is very similar, the only difference is: there must be an instance method in the first place parameter self.

 

 

 

 

 self: stand-instance argument

The first point: as long as the method is created in the class with def, we must take the first argument position left to self, and ignore it when it is called (do not pass parameters to the self).

Second: When calling class properties or other methods within the class methods thought necessary to use self property name or method name format self...

If you want to call other methods within the class method, we also need to use selfto represent instances

 

 

 initialize: initialize method

Format: def __init __ (self)

Role is: When an object is created for each instance, the code within the method need not call will automatically run.

 

 

 In addition to setting a fixed constant, the initialization method also may receive other parameters, so that incoming data can flow between the methods of the class as a property.

Incoming data can be called multiple times

 

 

 

 

Package for multiple

 

 

input used to create an instance method

 Creating a robot, let it have the following functions:
First, to make you ta named, will ask your name, then say hello to you (such as "Hello, I am Wu Feng Wali meet you, nice... ");
the second is a desire to make you say, then repeated three times to help you

 

Guess you like

Origin www.cnblogs.com/cloud7777/p/11628680.html