(7) Object-oriented foundation (1)

Object Oriented Basics
  • Process-oriented: write code from top to bottom according to business logic
  • Object-oriented: data and functions are bound together, and the classification is encapsulated. Each programmer only needs to be responsible for the classification assigned to him, so that the program can be developed more quickly and the repetitive code can be reduced.
  • object-oriented based on process-oriented
        Object-oriented (object-oriented; abbreviation: OO) has not yet had a unified concept. I hereby define it as: According to the systematic way of thinking that people understand the objective world, use the concept based on objects (entities) to build models, simulate the analysis of the objective world, Methods of designing and implementing software.
        Object Oriented Programming (OOP) is a design and programming method to solve software reuse. This method describes the similar operation logic and operation application data and state in the software system in the form of classes, and reuses them in the software system in the form of object instances, so as to improve the efficiency of software development.
 
kind
Objects are the core of object-oriented programming. In the process of using objects, in order to abstractly define a group of objects with common characteristics and behaviors, another new concept - class is proposed.
   
  The composition of a class: class name, properties, methods
  • class-name: class-name (installed camelCase)
  • Attributes of a class: a set of data
  • Class methods: methods (behaviors) that allow operations on   
A class can generate multiple objects, the objects are independent of each other, and each uses functions independently.
 
1. class Hero():     #Classic class

2. class Hero(object):   # New-style class definition form 
    def info(self):
         print ( " Heroes have their own opinions, so why ask the source. " )
# View the class's documentation, which is the class's annotations
print(Hero.__doc__)
 
  • There are 2 forms when defining classes: new-style classes and classic classes (python3 only has new-style classes)
  • object is the top-level parent class of all classes in Python;
  • info is an instance method, the first parameter is usually self, which means the instance object itself
  • The interpreter will pass the object on which the method is called as the first argument
 
magic method
    In the appropriate deeds will automatically run the method
    ① There are two underscores on both sides of the method name ② The names are all specified by the official
    # The method provided in the Python class, starting with two underscores and ending with two underscores, is the magic method 
    #If the __init__ method is not written on the class surface, Python will automatically create it, but it will not perform any 
    operations . To complete the function you want, you can define the __init__ method by yourself. #So 
    no matter whether you write the __init__ method in a class or not, there must be an __init__ method.

 

__init__(self) method:
Role: initialize the common properties of the object
After the object is created, it will automatically self, no need to manually call
__init__ can receive the required parameters after self (the actual parameters passed when the object was created, and the formal parameters passed back to the method)
 
__str__(self) method: Attachment: Only the self parameter can be received in parentheses
Role: print the description information of the object
Automatically when printing objects
The __str__(self) method must have a return value (return) and be a string
 
Attachment: When using print to output an object, the memory address of the object is printed by default. If the class defines the __str__(self) method, then the data returned from this method will be printed
 
__del__(self) method:
Role: verify whether the object is destroyed, release resources
Executed when the object is destroyed
 
__new__(cls) method:
Role: Executed when an object is created, used to control whether the current class can create new objects
__new__ must have at least one parameter cls, representing the class to be instantiated, this parameter is automatically provided by the Python interpreter when instantiating
To create a new object, you need to call the object's __new__ method to process
The new object created must return
The parameters for creating an object are not only given to __init__, but also to the __new__ method
 
 
 
{Summarize}
  • Outside of the class it is best to access properties through methods rather than directly
  • Private property: write two underscores before the property name, this property can only be used in the defined class
  • Private properties can be accessed from the inside of the class, but not from the outside
Attachment: eval: Strings can be executed as code
       repr: The definition format of the string can be obtained
       dir: can view all properties of an object

Guess you like

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