Object-oriented, and the like objects

Object-oriented concepts
  1, object-oriented programming --Object Oriented Programming, referred to as OOP, is a programming idea.
  2, it is the object program as a basic unit. The program data and processing the data package to the object

    • Advantages and disadvantages of
      advantages: ① good ② good flexibility and scalability, reusability ③ good shortcomings: ① ② increase the complexity of the program can not predict the results
    • Use scenario
      for high scalability required procedures, usually directly to the user, e.g. qq, micro-Letter
    • Process-oriented programming ideas
      core concern is the process, step by step process is the execution of advantages: logical, simplify complex issues, streamline
      Disadvantages: poor scalability, maintainability usage scenarios: low scalability requirements of the program, such as the kernel calculator
      Note:
      not all program objects must be oriented to analyze specific issues
  • The concept of classes and objects
    • Class definition:
      either type, class, an abstract concept is a set of features and the same behavior with the same object
    • Definition of an object:
      a thing is a concrete existence, have their own characteristics and behavior of an object is a combination of characteristics and skills
    • Relationship classes and objects:
      class contains a series of objects, the object belongs to a class;
      in life is to have an object class and then there is the first in the program in order to have an object class, we kind of have to tell the computer;
      What features of the object behavior summed up what a conclusion:
      when using the object-oriented programming, the first step is the need to think what kind of objects, objects have what kind of features and behavior,
      which summed up the type of information required in accordance with
  • How to create classes and objects
    • The definition of class
      name of class class: 
          content # class describes the attributes and skills 
          # description attribute with variable 
          # describe the behavior of a function 
      
      # class name written specification above all see the name known Italian names are big hump nomenclature   
      # Hump is the word capitalized large hump is the first letter capitalized, small hump is the first letter lowercase
    • Create Object
      Person class: 
          Pass 
      
      after # definition of a good class, you can create objects based on the Person class Person 
      p = Person ()
  • Design attributes and search order
    • Properties design
      properties can be written in the class class property, is common to all objects 
      can also be written in an object object properties are unique (not the same) for each object
    • Find the order of attributes
      if there are the same attributes, first access the object if there is no access to classes in the class and object 
    • Exercise
      Exercise: Describe a class teacher needs to contain a public property (schools) and a unique attribute (name) 
      
      class Teacher: 
          School = "Oldboy" 
          
          DEF __init __ (Self, name, Age): 
          self.name = name 
          self.age = Age 
          
      Teacher = T1 () 
      t1.name = "Jack" 
      t1.age = 28
  • Deletions attribute change search
    Increase property 
    object variable name. Attribute name = attribute value 
    
    to delete attribute 
    del Object Name Property Name del stu1.age 
    
    modify 
    objects. Attribute name = value of the new 
    
    view properties 
    print (Object .__ dict__) # access that object's properties  
    
    to access the object in that inside the class 
    print (Object .__ class__) # <class '__main   __. Teacher'>
  • Initialization method init
    • Definition:
      a function for setting an initial value for the object's properties
    • Features:
      Features 1 :: when instantiating the object, it will automatically execute the init method
      Feature 2: automatically object as the first argument, the name of the parameter bit self, self can be another name, but it is not recommended to change
      the characteristics of 3 : You can not have a return value
    • Exercise
      # Exercise: Create a class with several properties to set the property to him by the initialization method 
      
      class Dog: 
          DEF __init __ (Self, kind, Color, Age): 
              self.kind = kind 
              self.color = Color 
              self.age Age = 
      
      d1 = Dog ( "two ha", "black and white",. 1) 
      D1 = Dog ( "Teddy", "brown", 2) 
      
      
      Note: this function does not have any return value / None predetermined so .... only. . 
    • Note: The
      essence of the object function is to integrate data and process the data together,
      so to get an object to get the data while the data to be processed and the processing function
  • Binding and non-binding method method
    The default method in the case of class objects are bound method
    that is special because:
    when the function is called using the object will automatically pass the object itself, as the first argument
    when using the class name when he is to call a general function, there are several parameters have to pass a few parameters
    • Binding class method
      class binding method @classmethod to decorate
      special features:
      regardless of the call with a class or object will automatically incoming class itself, as the first argument
    • Non-binding approach
      . Or called a static method, that is, data that does not need to access the class does not need to access the object's data
      syntax: @staticmethod
    • How to determine the binding and to whom
      and when binding to the object: When the function logic required to access the data object
      when binding to the class: When the data logic function needs to access class

 

Guess you like

Origin www.cnblogs.com/buzaiyicheng/p/11239358.html