Object Oriented Programming (for review)

Three programming paradigms: object-oriented programming, object-oriented programming, functional programming .

Class : A class that integrates the same characteristics and actions of a class of things together. abstraction.

Object : A specific thing created based on a class.

Instantiation : The process of producing an object from a class is instantiation.

For example, the class defines some features and actions, then 98K is a type of sniper rifle, the feature has great lethality, and it is equipped with an eight-fold mirror. The m24 also has these features, except that the name is different. It is possible to combine the commonalities and characteristics of Shuanyu together. Pack them all up.

So, object-oriented design : integrating the actions and data of a specific class of things.

Object-oriented programming : implement object-oriented design by defining classes + instances.

#self is actually a dictionary form
 #declare that the class is similar to the function
 #class class name : ( no parentheses can be followed , the first letter of the class name is capitalized )
 # ' class docstring '
 # ' class body '
 # instantiation
 # d1 = Data () #The parentheses are the running class, instantiated, and parameters have not been passed
 in #The meaning of instantiation:
 # Attribute
 # The instance is generated by the class, then the instance should get the attributes of the class.
#So , what properties do classes generally contain?
#Data attribute: It is equivalent to a feature, such as the name type , these are obtained through variables, so they are variables.
#Function attribute: function, which is a method in a class.
# Both classes and objects use dots to access their properties. Hold back this sentence.
#print(dir(class)) View data attribute names and function attribute names, as well as built-in attribute names.
#print(class.__dir__) #



Looking at the attribute dictionary of the class, you can see that both data attributes and function attributes exist in this dictionary
 # In this dictionary, the key is the attribute name, and the value is the attribute value.
#Then , the essence of class.funcname() is to find something in the function dictionary.
# Object-related
 # Instantiation: The process of generating an object from a class.
#p1 = class(*args)   This process is instantiation, and p1 is the instance. Remember parentheses.
#In the class, there must be an initialization function to customize the properties of each object. __init__ .
#( What does this mean? That is , 98k and m24 are different things. I instantiate the class of sniper rifle and get 98k and m24 objects with different names and different
 powers. #Then the incoming parameters are different. These parameters need to be You can call the property after initializing it in the class. For example, pass in 98k , and then initialize to get the name, this name
 #

Get its power and ballistics in the method of the class, so it definitely needs to be initialized, and the method of not initializing the class will be bad. __init__ ( parameters of the instance )
 #In the class, the definition of __init__ will automatically return to a value attribute.
#__init__ must have a self attribute name first, and then the incoming parameter. For example , self.name is to pass in the name attribute to self , which is a numerical attribute. The
 #name attribute is encapsulated in self , and then returns the self dictionary by default , and name becomes a numerical attribute, name:value .
#The essence of the instantiation process is to call init.print(p1.__dict__) . So, the instance is init .
#Call print(p1.name) because name is a numerical attribute, it can be called by dot,The name attribute corresponds to a value , and the value corresponding to the name will be displayed .
#So , if you call class.func , if there are no parentheses after it, it is the memory address of the function to call. To get the value, you need to add parentheses and parameters to execute it.
#Instance has no function attribute, but it can be called from the class. Because of the scope, it will look outside init to find the class layer.
#How to adjust? p1.function() calls the method from the instance, which is equivalent to passing p1 to function from class.func(p1) .
#self represents the instance itself.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324607776&siteId=291194637