Guide:
1. Class methods
2. Static methods
3. Class method, instance method, static method
1.
class Methods
The method owned by the class object.
You need to use the adorner @classmethod to identify it as a class method, and for a class method, the first argument must be a class object, typically with the CLS as the first parameter.
class method Invocation:
class name . class Method ()
Example:
class Dog (object): __type " Dog " # class methods, decorated with Classmethod @classmethod def Get_type (CLS): return cls. __typePrint(Dog.get_type ())
Usage scenarios:
Class methods are defined when the method requires the use of class objects, such as accessing private class properties, and so on.
Class methods are typically used in conjunction with class properties. Class methods can access class properties, but cannot access instance properties.
Invocation of a class method
- Class name. class method Name ()
- Instance object. Class method Name ()--not recommended
2.
static methods
There are often methods in a class that are related to classes but do not change the state of classes and instances, which is a static method.
It needs to be decorated with the adorner @staticmethod , and the static method does not need to pass the class object or pass the instance object (the formal parameter is not self/cls), only need to receive our own parameters.
Static methods can also be accessed through instance objects and class objects.
static method invocation:
class name . Static Methods ()
Example:
classDog (object): Type="Dog" def __init__(self): name=None#Static Methods@staticmethoddefIntroduce ():#static methods do not automatically pass instance objects and class objects Print("a canine mammal, belonging to a carnivorous eye .") Dog1=Dog () dog.introduce ()#you can use class objects to invoke static methodsDog1.introduce ()#you can invoke a static method with an instance object
Usage scenarios:
Static methods are defined when the method does not need to use instance objects (such as instance objects, instance properties), or to use class objects such as class properties, class methods, create instances, and so on.
Eliminating unnecessary parameter passing can be useful in reducing needless memory usage and performance consumption.
To use class properties in a static method:
Class name. class Properties
3. Class method, instance method, static method
- The first parameter of a class method is the class object CLS;
- The first parameter of an instance method is the instance object self;
- Static methods do not have parameters that are forced to receive.
- When an object method, class method, or static method with the same name is defined in a class, the calling method takes precedence over the last defined method.
Example 1:
classDog:defDemo_method (self):Print("Object Methods") @classmethoddefDemo_method (CLS):Print("class Method") @staticmethoddefDemo_method ():#is last defined, and is invoked with priority execution Print("Static Methods") Dog1=Dog () Dog.demo_method ()#Result: Static methodDog1.demo_method ()#Result: Static method
Example 2:
classPerson (object):__num=0def __init__(self): person.__num+ = 1@classmethod#Decorative Device defGet_num (CLS):Print(CLS)#<class ' __main__. Person ' > returnCls.__num@staticmethoddefAdd2num (A, b):Print(A +b) LS=Person () Zs=Person ()Print(Person.get_num ())#2Person.add2num (11,22)# -
Python for small white 019: Object-Oriented-class method, static method