Python Path Object-oriented (i)

Source: Internet
Author: User
Tags class definition

Process-oriented vs object-oriented

The core of process-oriented is the process, which is the step to solve the problem.

Advantages: Greatly reduces the complexity of the writing program, just follow the steps to be executed, code codes can be.

Disadvantage: A set of procedures to solve a problem, lead the whole body.

The object-oriented core is the object.

Advantages: The extension of the program is solved, and the modification of a single object is mapped to the whole system.

Cons: Poor controllability, inability to predict results as accurately as the process can, and object-oriented programs that, once started, solve problems by interacting with objects, and cannot predict results.

Classes and objects (instance)


All objects in Python, the nature of the type is the class, including dictionaries, lists, numbers, strings, and so on.

In Python, a variable represents a feature, a function representation method, a class of things with the same characteristics and methods even "classes", the object is the specific one in the class.

Class


Define a class

class Class_name:  #classkeyword, class Nade letter capital, followed by Colon    pass

declaring classes

class Person :     "  Person " # Property static Property    def Walk (self):         Print ("person iswalking ... ") # Method Dynamic Properties

Two functions of a class: Property Reference and instantiation

Property Reference (class name . properties)

 class   person:role  = "  person   " #   def   Walk (self):  print  ("  person is walking ...   ") #   method dynamic Property  print  (Person.role) #   View the properties of role  print  (Person.walk) #   Walk method of the reference person  

Instantiation: The class name parentheses are instantiated, automatically triggering the run of the __inti__ method, which you can use to customize your own characteristics for each instance.

 class   person:role  = "  person   " #   def  __init__   (self,name): Self.name  =< Span style= "COLOR: #000000" > name  def   walk ( Self):  print  ( " person is walking ...   ") #   method dynamic Property  print   (person.role)  print  (Person.walk) 

Instantiation is the process by which a class produces an object.

Special class Properties

  Two: Special Class attribute class name.  __name__  #   class name. __doc__  #   class name. __base__  #   class name. __bases__  #   class name. __dict__  #   class name. __module__  #   class name. __class__  #   
View Code

Object is an example of the actual existence of a class, that is, an instance, an object that has only one function, a property reference

classPerson :" "define a class of people" "role=" Person"#Property Static Property    def __init__(self,name,aggr,life_value): Self.name=name Self.aggr=Aggr Self.life_value=Life_valuedefAttack (Self,dog): Dog.life_value-=Self.aggregg= Person ("Egon", 100,1000)#Property ReferencePrint(Egg.name)Print(EGG.AGGR)Print(Egg.life_value)

Use a function to interpret this class:

defPerson (*args,**Kwargs): self= {}    def __init__(name,aggr,life_value): self["name"] =name self["Aggr"] =Aggr self["Life_value"] =Life_valuereturn Selfreturn __init__(*args,**Kwargs) Egg= Person ("Egon", 10,20)Print(egg)

Fixed mode for definition and invocation

classclass Name:def __init__(self, parameter 1, parameter 2): Self. Property of Object 1=parameter 1 self. Properties of the Object 2=Parameter 2defMethod Name (self):Pass    defMethod Name 2 (self):PassObject Name= Class Name ($)#An object is an instance that represents a specific thing.                  #class name (): 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, others correspond to parameter one by one in Init                  #The result returns an objectObject name. object's Properties 1#View the properties of an object, directly using the object name. Property nameThe name of the object. Method Name ()#call a method in the class, directly with the object name. Method Name ()Summary
Fixed mode

Object Interaction:

classPerson:#Define a humanRole ="Chinese"    def __init__(SELF,NAME,LIFE_VALUE,AGGR): Self.name=name Self.life_value=Life_value Self.aggr=AggrdefAttack (Self,dog): Dog.life_value-=Self.aggrclassDog:#define a sufficient class    def __init__(SELF,NAME,LIFE_VALUE,AGGR): Self.name=name Self.life_value=Life_value Self.aggr=AggrdefBite (Self,person): Person.life_value-=Self.aggregg= Person ("Egon", 500,50) Dog= Dog ("Teddy", 20000,500)#Egon attacks the dog, the dog's health is less thanPrint(Dog.life_value) egg.attack (dog)Print(Dog.life_value)#dog Bites Egon,egon's life is less than the number ofPrint(egg.life_value) dog.bite (egg)Print(Egg.life_value)#Egg has 200 bucks to buy the gear.Egg.money = 200classWeapon:#define a weapon class    def __init__(Self,name,price,aggr,life_value,attack_force): Self.name=name Self.price=Price Self.life_value= Life_value#Life BonusSelf.attack_force = Attack_force#AttackSelf.aggr = Aggr#Damage Bonus    defUpdate (Self,person):#with equipmentPerson.money-=Self.price Person.life_value+=Self.life_value Person.aggr+=Self.aggrdefKill (self,obj):#obj represents the attacker's big strokesObj.life_value-=Self.attack_force playing dog sticks= Weapon ("Playing dog sticks", 199,50,50,10000)ifEgg.money >hit a dog stick. Price: Hit a dog stick. Update (egg) egg. Weapon=Playing dog sticks#Egon's attack and health values are increased byPrint(Dog.life_value)Print(Egg.life_value) egg.attack (dog)Print(Dog.life_value)#Egon Amplification recruit, dog's health reduced 10000Print(Dog.life_value)Print(egg.life_value) egg. Weapon.kill (dog)Print(Dog.life_value)
Man and Dog wars

class namespaces and instance namespaces

When you create a class, you create a namespace for a class

A static property is a variable that is defined directly in the class

A dynamic property is a method defined in a class

The data for the class is shared to all objects.

The dynamic properties of a class are bound to all objects.

Creating an instance creates an object/instance namespace that holds the name of the instance, called the attribute of the instance.

Python Path Object-oriented (i)

Related Article

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.