Object-oriented advanced features

Class instance attributes and attribute

Class attribute defines the
class attribute is the class object owned property, which is shared by all the class of the object instance of the object, there is only one copy in memory.
In the previous example we come into contact is an instance attribute (object attribute), it is not common to all instances of objects of an object class, the number of copies in memory depends on the number of objects.
FIG experimental results are as follows:
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Applications class attributes

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Static methods and class methods

The method defined class
class method is a class object owned, need to modify @classmethod generally identified as a class method,
1). For a class method, the first parameter must be a class object, as the first parameter
( cls a parameter, a variable name can be changed to another, but preferably with 'cls' a)
2). by way of example able to access the object and the object class.
Static methods defined
static methods need to modify @staticmethod generally identified as a static method,
1). The method does not require the static definition of multi-parameter
2). By way of example able to access the object and the object class.
Experimental results is shown below:
Here Insert Picture Description
Here Insert Picture Description
Property Method: This method can modify and delete statements such as easier
Here Insert Picture Description
Here Insert Picture Description

Singleton

For some classes of systems, only one instance is important, for example, a system can contain multiple print job, but can only have a job are working; a system can be only one window manager or file system ; a system can have a timing device or ID (number) generator. As in Windows, you can only open a task manager. If you do not use the mechanism of the window object uniquely, multiple windows pop up, if the contents of these windows display exactly the same, it is the duplicate object, a waste of memory resources; inconsistent content if the window is displayed, it means that in a moment the system has multiple states, inconsistent with the actual, misunderstanding will bring to the user, we do not know which one is the real state. Thus the system may ensure a unique class of an object that is only a very important example of
the experimental results is as follows:
decorative Singleton implements
Here Insert Picture Description
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_43592835/article/details/92962482