Python classes and objects (encapsulation)

If the meaning of function is to repeat the same operation we need anytime and anywhere, then the meaning of encapsulation is to create the same type of object anytime and anywhere, which is the biggest embodiment of python's object-oriented characteristics.

Classes are similar to object constructors, or "blueprints" for creating objects.

1. Create a class

The keyword class can be used to create a class.

Examples are as follows:

class pet:
    type = 'dog'

Such a pet class is created, the name of this class is pet, and type is an attribute of this class. But it is not commonly used to create a class in this way, because the attribute of this type will become the same attribute of the object created through this class, but pets are not only dogs, if we want to create one that we can input instance parameters by ourselves How to set the attribute of type? Then we need to introduce the first function method __init__ function in the class.

1. __init__ function (for the assignment of attributes in the class)

All classes have a function called __init__() which is always executed when the class is started.

Use the __init__() function to assign values ​​to object properties, or for other operations that need to be performed when an object is created.

Examples are as follows:

class pet:
    def __init__(self,type):
        self.type = type

self is the first parameter of the __init__ function, and it is also an essential parameter. The meaning of this parameter is to distinguish different objects created in the same class. It can be used as a formal parameter, but this formal parameter can not Enter the instance parameter, your object name is the instance parameter of this formal parameter.

When creating all properties for this function, self. should be prefixed to indicate the object to which this property belongs. In the creation of all functions behind this class, if you want to call the attribute of this object, you should add self. before the name of the called attribute to indicate which object's attribute is called. 

Note: This parameter name does not necessarily have to use the name self when creating a class, you can change it to any name you like, but generally speaking, everyone will like to use self.

2. Other built-in functions

If the __init__ function is to add attributes to the object, then the built-in functions added later can represent various behaviors of this class.

Examples are as follows:

class pet:
    def __init__(self,type,name):
        self.type = type
        self.name = name
    def running(self):
        print(f"{self.name} is runing.")

For example, we add a running built-in function to this class. When we want the object created with this class to run, we can call this function to indicate the running behavior of this object.

Note: New parameters can be added or not added to other built-in functions, but the self parameter is still essential. The self parameter indicates whose property you are calling. Therefore, in the settings of all built-in functions later , should add the parameter self.

2. Create objects 

The class has been created, and the next step is to use the class to create objects.

Examples are as follows:

my_dog = pet(dog,jack)
my.dog.running()

The output is as follows:

jack is running

You first need to create an object to receive the instance created by using this class. The instance parameter is entered in the brackets after the class name, and the self parameter can be omitted. If you want to use the built-in function in the class, you need to specify this The object to which the built-in function belongs is then called.

Three, subclasses and parent classes 

Classes are also divided into sub-categories and major categories. For example, we just created a pet category, but if we want to create a black pet category, it also belongs to the category of pets, but the fur color of all objects in this category is Black, then we need to introduce the concept of a subclass and a parent class.

The subclass and the parent class are relative. The subclass is only a subclass relative to its parent class, and the parent class is only a parent class relative to its subclass. The subclass can inherit all the properties and methods of the parent class, then In the operation of creating subclasses, we involved an operation called inheritance.

1. Inheritance

Inheritance means that the subclass inherits all the methods and attributes from the parent class. If you want a subclass to inherit the parent class, when creating the subclass, enter the parent class as an instance parameter into the creation of the subclass.

Examples are as follows:

class black_pet(pet):
    pass

This completes the creation of the black pet subclass, which will inherit all the properties and functions of the parent class pet. If you don’t want to add other properties and methods, you can use the pass keyword, because creating a class without performing any operations is Not allowed, and the pass keyword can prevent the system from reporting an error.

2. Use the __init__ function in the subclass

If the __init__ function is used in the subclass, because the __init__ function will be called immediately when the object is created, then if the __init__ function is added to the subclass, then the attributes set in the __init__ function of the parent class Will be overridden in the subclass, which means that the subclass no longer inherits the properties of the parent class, but the methods in the parent class can still be used.

But sometimes we need properties of the parent class, but we also want to add properties specific to the subclass compared to the parent class, so we can use the super function.

3. super function

The super function allows the subclass to inherit all the properties and methods of the parent class.

If the subclass wants to use the super function to inherit the parent class, the parameters of the __init__ function of the subclass need to transplant the parameters of the __init__ function of the parent class to the subclass first, and then choose whether to add new parameters, If there are 5 parameters in the __init__ function of the parent class, and the __init__ function of the subclass only has four of the parent class, python will report an error.

Examples are as follows:

class black_pet(pet):
    def __init__(self,type,name)
    super().__init__(type,name)
    self.color = 'black'

In this way, we not only let the subclass inherit the properties and methods of the parent class, but also increase the unique properties of the subclass.

Note: When using the super function, the self parameter is not required in the parameters. Only the parameters of the built-in functions in the class need the self parameter, and the self function is not a built-in function. 

Guess you like

Origin blog.csdn.net/m0_74043494/article/details/127945486