Note: The following are public-premise, private methods can only be called inside the class, do not need to speak more.
1. Object methods
This method has a default parameter: Self, which represents the object of the instance
def __init__ (self): Print (" Initialize object ")
Class is not a direct call to an object method:
class User (object): ' ZS ' def __init__ (self): Print (" Initialize object ") of User. __init__ ()
This call throws an error: TypeError: __init__ () Missing 1 required positional argument: ' Self '
2. Class methods
The class method, as the name implies, can be called directly from the class name, or by an object instantiated by the class.
classUser (object): Name='ZS' __pass= 1223def __init__(self):Print("Initializing Objects") @classmethod#Decorator def changeName1(CLS): Cls.name='ls' Print("method%s for class"% (CLS.__pass))
ChangeName1 is a class method, preceded by a decorator: @classmethod, and has a default parameter, the CLS is the class itself.
3. Static method
In fact, static methods can be understood as a special class method, which differs from the normal class method in two points:
(1) Different modifiers
(2) No default parameters
The rest is identical to the class method.
classUser (object): Name='ZS' __pass= 1223def __init__(self):Print("Initializing Objects") @classmethod#Decorator defchangeName1 (CLS): Cls.name='ls' Print("method%s for class"% (CLS.__pass)) @staticmethoddefchangeName2 (): User.Name='ww' Print("Static Methods") U=User () u.changename1 () user.changename1 () u.changename2 () user.changename2 ()
Summary: An object instantiated by a class can invoke all public methods inside the class, and the class can only invoke the class method and the static method (if it is a private method, it can only be called inside the class. )
Class methods, static methods, object methods in Python