>>> Python class properties (and private property, public property, property instance, local variables) class method (Example methods, static methods)

First, create a class:

 Class's definition of an object that contains information about the object of the action the way, including its name, methods, properties, and events. Class does not exist in memory, so it is not itself an object. When a program needs to run code that references the class, creates a new instance of a class in memory, that object. Although only one class, but it can create multiple objects of the same type in memory for this class

class the Person (Object): 

    # method class must have a self argument, but when the method is called, do not pass this parameter 
    DEF get_name (self):      
         Print  " My name IS: james " 

    DEF get_age (self):
         Print  " Age iS My: 3, 5 " 

    DEF get_hoppy (Self):
         Print  " My Hoppy iS: lvyou " 
Boy = the Person () 
boy.get_name ()        # However, when the method is called, do not pass this parameter 
boy.get_age ()         # in when you call get_age other methods, boy automatically as an argument own approach, so we call it Self 
boy.get_hoppy ()

Second, the package type and polymorphism

Package: abstract soon obtained data and behavior (function) are combined to form an organic whole, i.e. the data source and data organic integration operation, forming class.

Where the data and functions are members of the class. The purpose is to hide the object's properties and implementation details publicly available interface control attributes in the program to read and modify access levels.

Encapsulation and polymorphism difference: Polymorphism allows users to not know what class is the object method call, and the package is not concerned about how objects are created and directly use

class MyClass (Object): 

    # define a global variable 
    name = ' HTML ' 

    DEF set_name (Self, name): 
        the self.name = name 

    DEF get_name (Self):
         return the self.name 

My = MyClass () 
my.set_name ( ' kryle ' )
 Print my.get_name () 

# if the variable name is stored in global variables, after MyClass instantiated, will change the global variable name 
My = MyClass () 
my.name = ' Tom ' 
Print my.get_name () 

#Object has its own state, the state of the object is described by its characteristic name, eg: the content of the following objects she does not affect the contents of my objects 
she = MyClass () 
she.set_name ( ' Angel ' )
 Print she.get_name ()
 Print my.get_name ()

Third, the class method, and classification defined attribute:

python class classification methods and properties:

Class method: public methods, private methods, class methods, static methods

Class attributes: Attribute private, public property, property instance, local variables

Variables class is divided into public and private variables

[Python class contains methods]

Public methods: Method in class and outside the class can call

Private methods: You can not be invoked outside the class, in front of the method with "__" double underscore is the private method

Class Methods: The classmethod () function treated function, the class can be called, can also be called by the object (inherited relations)

Static method: the equivalent of "global function", the class can be called directly, can be shared by all objects instantiated by staticmethod () is defined, the static method does not "self" parameter.

Built-in functions: __ xxx__ system definition name, both before and after a "double underline" represents the python in a special way to identify specific, as __init __ () representative of the class constructor.

The main difference is that the parameters of the three methods, an example method is bound to an example, can be called only by way of example; however, for static methods and class methods can be called by the class name and instance two ways

self Parameters:
a discrimination method functions and classes (there must be a self), self parameter indicates the execution target itself

[Python class includes properties]

  • Private property

    Function, method or property name begins with two underscores, for the private type;

  • Public property

    If the function, or a method attribute name does not begin with two underscores, for the public property;

  • Instance Properties

    In self prefixed attributes;

  • Local variables

    Method variables defined in the class is not used as a prefix self declaration, the variable is local;

Detailed description:

xxx public variable

_xxx "single underline" member variables to start is called to protect variable, meaning that only the class object (ie class instances) and subclass object they can have access to these variables, you need an interface through class provides access; can not use 'from module import * 'import

__xxx class private variables (Python functions are objects, so the members of the method known as member variables also work.) "double underline" the beginning of a private members, meaning that only he can access the class object, even subclass object also not have access to this data, but it can be accessed by way of a private property by instance._classname_attribute

tip: python in a little pseudo-private attributes mean

Fourth, the difference

classmethod is a class method, and staticmethod is a static method.

In Python, static methods and class methods are accessed through the class object can be instantiated and class objects. But the difference is:
@classmethod modifier is a function that represents the next first parameter cls a class method, class method, the first parameter is example of a method of self, showing an instance of the class.

Common Object process requires at least a self parameter representative instance of the class object, class methods are class variables cls passed, can be used to do some processing related cls. And when there is a subclass inheritance, when calling such methods, the incoming class is a subclass of cls variable, rather than the parent class.

For class methods can be invoked by the class, for example, is a class A, then we can call inside method A method A.method (), can also be invoked by an instance of the class, such as A (). Method () call, first of all a () method calls the initialization method of an object instance a is an a, and then call the method through which the method of the object.

Fives,

Six examples

Method of distinguishing classes:

# - * - Coding: UTF-. 8 - * - 

class People (Object):
Color = "Yellow" # global public variables
Default_name = 'the anyone'
__age = 30 # private attributes

within #python the constructor is
def __init __ (self) :
the self.name = 'someone'
#python instance of the function of S
DEF Think (Self):
self.color = "Black"
Print ( "% S the I AM A" self.color%)
Print (Self .__ Age)

DEF __talk ( Self):
Print ( "Talking with the I AM Tom")

# DEF Test (Self):
# Print self.color
@classmethod # decorators, only a function of the following functions
def class_method (cls, message): # class method
print ( "class method performed class_method (% s,% s) "% (cls, message))
# Print People.color # access class inside other members

@staticmethod # decorator, a function only works with the following
def static_method (): # class static methods
print ( "I'm a static method defined")


REN = People ( )
Print (REN)
Print (ren.color)
ren.think ()
Print (ren._People__age)
# private property call the method, it is recommended: use only test program
 
 
 

Part Quoted from: https: //blog.csdn.net/wohu1104/article/details/89424370 (quite good)

Guess you like

Origin www.cnblogs.com/clarenceyang/p/12593291.html