Python_oldboy_ automatic operation and maintenance of the road _ object-oriented 2 (10)

Source: Internet
Author: User

The content of this section:

    1. The origin of object-oriented programming
    2. What is object-oriented program design and why should it
    3. Classes and objects
    4. Inheritance and derivation
    5. Multi-state and polymorphism
    6. Packaging
    7. Static Methods and Class methods
    8. Object-oriented Software development

1. The origin of object-oriented programming

See Overview: Http://www.cnblogs.com/linhaifeng/articles/6428835.html

2. What is object-oriented program design and why should it

Process-oriented program design is the core of the process, the process is to solve the problem of the steps, process-oriented design is like a well-designed pipeline, consider when to deal with what things.

The advantages are: greatly reducing the complexity of the program

The disadvantage is: a set of pipeline or process is to solve a problem, the production line of soda can not produce a car, even if it can, also have to be big change, change a component, reaching.

Application scenario: Once you have completed a very rarely changed scenario, the notable examples are Linux kernel, git, and Apache HTTP server.

Object-oriented program design is the core of the object, to understand what the object, we must regard themselves as God, God in the eyes of the world is the object of the existence of all things, 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 data properties and method properties, but this is not fun, so the Tathagata arranged a group of ghosts and goblins, in order to prevent the four of them in the road of learning to be killed, and arranged a group of immortal escort, these are objects. Then the beginning of the lessons, master and disciple four people with ghosts and goblins to interact until the final canon. The Tathagata does not control the four people follow what process to take.

Object-oriented program design

The advantage is that 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 requirements of the software, the general needs 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

Object-oriented programming is not all. For a software quality, object-oriented programming is just to solve extensibility.

3. Classes and Objects

Everything in Python is an object, and Python3 unifies the concept of class and type, and the type is class, so whether you believe it or not, you've been using classes for a long time.

>>> Dict#type dict is Class Dict<class 'Dict'>>>> D=dict (name='Lijun')#instantiation of>>>d{'name':'Lijun'}>>> D.pop ('name')#send a message to D that executes the method pop of D, which is equivalent to calling a property of the Dict class, where pop is the function to delete the specified contents of the dictionary and Echo'Lijun'

Based on object-oriented design a game: League of Legends, each player to choose a hero, each hero has its own characteristics and skills, characteristics that are data attributes, skill is the method of attributes, characteristics and skills of the combination of an object.

Extracting similar parts from a set of objects is a class , which is also a combination of features and skills, characterized by data and data shared by all objects, skill is a function attribute and is a function attribute shared by all objects.

Garen_hero. Q () is called to Garen_hero this object sends a message, let him go to perform q This function, complete a function, similar to have:

Garen_hero. W ()

Garen_hero. E ()

Garen_hero. R ()

A hero can attack another hero, which is the interaction between objects.

Garen_hero.attack (Riven)

#class has two functions: instantiation and attribute ReferenceclassGaren:#lol game characters Galencamp='Demacia'      #Camp: Demacia    def __init__(Self,nickname,aggresivity,life_value):#Each player has a common attribute: Name, attack, healthSelf.nickname =Nickname Self.aggresivity=aggresivity Self.life_value=Life_valuedefAttack (Self,enemy):#defining an attack's properties        Print('Is attacking ....', Self,enemy)#the first function of a class: instantiationG1 = Garen ('Bush LUN', 82,100)#instantiate, which is equivalent to generating a gamerG2 = Garen ('hahaha', 12,1020)#second function of the class: Property reference, containing data properties and function PropertiesPrint(Garen.camp)Print(Garen.__init__)Print(Garen.attack)#for an instance, there is only one function: Property ReferencePrint(G1.nickname,g1.aggresivity,g1.life_value)#Print(G1.attack,id (G1.attack))#invoking a function in a class from an instance, depending on what is displayed is the binding methodPrint(Garen.attack,id (Garen.attack))#calling your own function properties through a class#Summary:#for classes is the combination of data (camp) and functions (attack)#for the data property of a class, shared to the instance#for a function of a class, bound to an instance, bound to use for instance, the instance can pass its own parametersGaren.attack (The) G1.attack ('Lijun')#equivalent: Garen.attack (G1, ' Lijun ') This is the good use of the binding method, automatic transfer of valuePrint(G1.camp,id (g1.camp))Print(G2.camp,id (g2.camp))Print(Garen.camp,id (Garen.camp))
Classes and objects
#-*-coding:utf-8-*-#blog:http://www.cnblogs.com/linux-chenyang/##class embodies the concept of object-to-object interactionclassGaren:#lol game characters Galencamp='Demacia'      #Camp: Demacia    def __init__(Self,nickname,aggresivity,life_value):#Each player has a common attribute: Name, attack, healthSelf.nickname =Nickname Self.aggresivity=aggresivity Self.life_value=Life_valuedefAttack (Self,enemy):#defining an attack's properties        Print('Is attacking ....', Self,enemy)classRiven:camp='Noxus'    def __init__(Self, nickname, Aggresivity, Life_value):#Each player has a common attribute: Name, attack, healthSelf.nickname =Nickname Self.aggresivity=aggresivity Self.life_value=Life_valuedefAttack (self, Enemy):#defining an attack's properties        Print('Is attacking ....', Self, enemy) Enemy.life_value-= Self.aggresivity#G1 's last health is G1 's current life minus R1 's attack.G1= Garen ('Bush LUN', 82,100) R1= Riven ('Hahah', 20,1000)#simulate Riven heroes attack Garen HeroesR1.attack (G1)Print(G1.life_value)#so the last G1 of life is 100-20.
Object Invocation
#-*-coding:utf-8-*-#blog:http://www.cnblogs.com/linux-chenyang/##the process of writing a class: Give a column, Nu wa made man#The first step: first think of what common properties, private properties, common skills, first stop instance of the angle to think#Step Two: write when you start writing class, and then in the build instanceclassChinese:dang='Gongchandang'                              #Common Properties        def __init__(Self,name,age,gender):#Private PropertiesSelf.name =name Self.age=Age Self.gender=GenderdefTalk (self):Print('chifan Shuijiao Dadoudou ...')#Common SkillsD=chinese ('Lijun', 18,'F')
the idea of writing classes

Python_oldboy_ automatic operation and maintenance of the road _ object-oriented 2 (10)

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.