Python 2018.7.24 class space, object space, query order, combination

Source: Internet
Author: User

Query Order:
Object. Properties: First find from Object space, if not found, then from the class space to find, and then find, and then from the parent class to find ....
Class name. Properties: First find from this type of space, if not found, then from the parent class to find ....
Objects are independent of each other from objects.

Calculates how many objects an instance of a class instantiates.
Cases
    Class Count:
Count = 0
def __init__ (self):
Count.count = Self.count + 1
Obj1 = Count ()
Obj2 = Count ()

Print (Count.count)
Count = 0

 class Count: 
Count = 0
def __init__ (self):
Pass

You can change the value of a static variable in my class by using the class name count.count = 6
Print (count.__dict__)

but cannot be changed by object can only refer to static variables in class

Obj1 = Count ()
Print (obj1.count)
Obj1.count = 6



combination: Encapsulates a property for an object of a class that is an object of another class.
Example
Version two:
Class Gamerole:
def __init__ (self, name, AD, HP):
Self.name = Name
Self.ad = AD
SELF.HP = HP

Def attack (Self,p):
P.HP = P.hp-self.ad
Print ('%s attack%s,%s lost%s blood, left%s blood '% (SELF.NAME,P.NAME,P.NAME,SELF.AD,P.HP))

def armament_weapon (Self,wea):
Self.wea = WEA


Class Weapon:
def __init__ (Self,name,ad):
Self.name = Name
Self.ad = AD
def fight (SELF,P1,P2):
P2.HP = P2.hp-self.ad
Print ('%s was hit with%s%s,%s lost%s blood, left%s blood ' \
% (P1.NAME,SELF.NAME,P2.NAME,P2.NAME,SELF.AD,P2.HP))

P1 = Gamerole (' Big Brother ', 20,500)
P2 = gamerole (' Indian ning ', 50,200)
Axe = Weapon (' kick ', 60)
Broadsword = Weapon (' Dragon Slayer Blades ', 100)
# Print (axe)
P1.armament_weapon (Axe) # equip Big Brother with kick this object.
# Print (P1.WEA)
# Print (p1.wea.name)
# Print (P1.WEA.AD)
P1.wea.fight (P1,P2)




Python 2018.7.24 class space, object space, query order, combination

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.