Object-oriented Python

Source: Internet
Author: User

Object Oriented

    • Object-oriented VS process-oriented
    • Process oriented
        • Process- oriented program design is the core of process (pipeline thinking), the process is the steps to solve the problem, process-oriented design is like a well-designed line, considering when to deal with something.
        • Advantage: Greatly reduce the complexity of the writing program, just follow the steps to be executed, stacking the code can be
        • Cons: A set of lines or processes is used to solve a problem, code reaching
        • Application scenario: Once you've completed a scenario with minimal change, the famous examples are Linux kernel, git, and Apache HTTP server.
    • Object oriented
        • The core of object-oriented program design is the object (God-like thinking ) to understand what the object is, must regard themselves as God, God in the eyes of the existence of all things are objects, not exist can also be created. Object-oriented programming good for example, to design a journey to the east, the Tathagata to solve the problem is to pass the scriptures to the Eastern Earth Datang, the Tathagata thought to solve this problem requires four people: Tang's monk, Sha Monk, Pig Eight commandments, Monkey King, everyone has their own characteristics and skills (this is the concept of objects, Characteristics and skills corresponding to the object's properties and methods, but this is not fun, so the Tathagata arranged a group of ghosts and goblins, in order to prevent the four people in the study on the road was killed, and arranged a group of immortal escort, these are objects. Then the beginning of the lessons, master and master Four with ghosts and demons to each other until the end of the canon. The Tathagata does not care what process the four people follow to fetch
        • Advantages: It solves the extensibility of the program. A single modification of an object is immediately reflected in the entire system, such as the character of a character parameter in the game and the ability to modify it easily.
        • Cons: Poor controllability, inability to process-oriented programming pipelining can accurately predict the problem of the processing process and results, the object-oriented program once started by the interaction between the object to solve the problem, even God can not predict the end result. So we often see a game of people changes in a certain parameter is likely to lead to the ability of the bully to appear, a knife to kill 3 people, the game is out of balance
        • Application scenario: frequently changing software, general requirements of the changes are concentrated in the user layer, Internet applications, enterprise internal software, games, etc. are the object-oriented programming of the good place
  • Initial classes and objects
      • Initial class
        • Statement:
          • # declaration function def functionname (args):     ' function document string '      function Body # declares class ' Class ' class name:    ' Document String ' class '    #我们创建一个类class Data:    passclass Person:   #定义一个人类    role = "Person"  #人的角色属性都是人    def Walk (self):  #人都可以走路, That is, there is a walking method, also called dynamic attribute        print ("Person is walking ...")
        • Two functions of a class: Property Reference and instantiation
          • Property reference (class name. Properties)
          • Class Person:   #定义一个人类    role = "Person"  #人的角色属性都是人    def Walk (self):  #人都可以走路, that is, there is a way        to walk Print ("Person is walking ...") print (Person.role)  #查看人的role属性print (person.walk)  #引用人的走路方法, note that this is not a call
          • Instantiation: Class name parentheses are instantiated, which automatically triggers the run of the __init__ function, which you can use to customize your own features for each instance
          •  class Person: #定义一个人类 role = ' person ' #人的角色属性都是人 def __init__ (self,name): Self.name = NA            Me # Each character has its own nickname;  def walk (self): #人都可以走路, that is, there is a way to walk print ("person is walking ...") print (Person.role) #查看人的role属性print (Person.walk) #引用人的走路方法, note that this is not a call to 
        • # The process of instantiating is a class-to-object procedure # Originally we had only one person class, in which a egg object was created with its own # specific name, attack, and health value. # Syntax: Object name = class name (parameter) egg = person (' Egon ')  #类名 () is equal to the execution of person.__init__ () #执行完__init__ () returns an object. This object is similar to a dictionary and has some properties and methods that belong to the person itself. #你可以偷偷的理解: Egg = {' name ': ' Egon ', ' Walk ': walk}# property called print (Egg.name)     #查看属性直接 object name. Property name Print (Egg.walk ())   #调用方法, The name of the object. Method Name ()
        • Supplement to class attributes
        • A: Where do the properties of the class we define are stored? There are two ways to view Dir (class name): A Name list class name is detected. __dict__: A dictionary is found, key is the property name, value is the attribute two: Special Class attribute class name. __name__# class Name (String) class name. __doc__# Class name of the document String class. The first parent class of the __base__# class (Speaking of inheritance) class name. A tuple of all the parent classes of the __bases__# class (Speaking of inheritance) class name. The class name of the dictionary attribute of the __dict__# class. The __module__# class defines the module class name. Class for __class__# instances (in modern class only)
    • Object
      • Object is an example of a class that actually exists, that is, an instance
      • Object/instance has only one function: Property reference
      • Class Person:  # defines a human    role = ' man '  # People's character attributes are people    def __init__ (self, name, aggressivity, Life_value): C4/>self.name = Name  # Each character has its own nickname;        self.aggressivity = aggressivity  # Each character has its own attack;        Self.life_value = Life_value  # Each character has its own health value;    Def attack (Self,dog):          # People can attack a dog, and the dog here is also an object.        # People attack the dog, then the dog's health will fall according to human attack        dog.life_value-= self.aggressivit# Instantiate egg = person (' Egon ', 10,1000) # Reference print ( Egg.name)     print (egg.aggressivity) print (Egg.life_value)
      • Object-Oriented Summary
      • Class Name:    def __init__ (self, parameter 1, parameter 2): Self        . Property of the Object 1 = parameter 1 self        . property of Object 2 = parameter 2    def method name (self):p    Def method Name 2 (self):p the name of the object = class name  #对象就是实例, which represents a specific thing                  #类名 (): Class name + parenthesis is the instantiation of a class, equivalent to calling the __init__ method                  # Arguments in parentheses, parameters do not need to pass self, the other corresponds to the parameter one by one in Init                  #结果返回一个对象对象名. The object's property 1 #查看对象的属性, directly with the object name. The name of the object.   method Name ()     #调用类中的方法, Directly with the object name. Method Name ()
  • Interaction between objects
    • Example
    • Class Person: # defines a human role = ' man ' # People's character attributes are People def __init__ (self, name, aggressivity, life_value): Self        . Name = name # Each character has its own nickname;        self.aggressivity = aggressivity # Each character has its own attack;    Self.life_value = Life_value # Each character has its own health value;        Def attack (Self,dog): # People can attack a dog, and the dog here is also an object.    # People attack the dog, then the dog's health will fall according to human attack Dog.life_value-= Self.aggressivityclass Dog: # define a dog role = ' dog ' # The character of a dog is a dog.        def __init__ (self, name, breed, aggressivity, life_value): Self.name = name # every dog has its own nickname;        Self.breed = breed # Every dog has its own breed;        self.aggressivity = aggressivity # Every dog has its own attack;    Self.life_value = Life_value # Every dog has its own health value;        def bite (Self,people): # Dogs can bite people, and dogs here are also an object. # The dog bites, then the human health will fall according to the attack of the dog people.life_value-= Self.aggressivityegg = Person (' Egon ', 10,1000) #创造了一个实实在在的人eggha2 = Dog (' Erlengzi ', ' Husky ', 10,1000) #创造了一只实实在在的狗ha2print (ha2.life_value) #看看ha2的生命值egg. Attack (HA2) #egg打了ha2一Lower print (ha2.life_value) #ha2掉了10点血 

Object-oriented Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.