python - Object Oriented base (classes, objects, the query sequence, composition)

A. Object-Oriented

1. categories: those things have the same attributes and skills

2. Object: specific performance class, a specific example of a real

    1. The cat is a class, who domesticated cat is a target

II. Application and operation class

1. Create a class

class the Person:   # Create a class by class, the class name capitalized

2. The name of the class angle

  1. Static variable class of operation

class the Person:   # create a class, the class name capitalized 
    Mind = ' idea '   # define a variable, static variable, static field 
    Faith = ' faith ' 
    DEF  __init__ (Self):
         Print ( ' today is full of vitality day ' ) 

    DEF Work (Self):   # methods, functions, dynamic variables 
        Print ( ' new day ' )
# Class name .__ dict__ query class everything. (CRUD operation can not be performed) 
Print (the Person. __Dict__ ) 
 # class name .__ dict __ [ 'static variables'] query static variables. (Not recommended) 
Print (the Person. __Dict__ [ ' Mind ' ])
# Universal. Class in a single variable CRUD operations 
Person.money = ' 600 '     # by 
del Person.faith         # puncturing 
Person.mind = ' thinking '     # change 
Print (Person.mind)       # Charles 
Print (the Person . __dict__ )

  2. The method of operation of the class

Person.work ( ' pass a parameter ' ) # call the method (not recommended)

3. Creating an object

The Person ()     # class name + (instantiated process), to create an object of the process (also called object space) 
        # As long as the class name + (), it will automatically execute class __init__ method

4. The angle of the object

  1. Static variable operation target

    __dict__ query for all content objects

    Everything was performed CRUD

    Object manipulation class static variable, can only be queried

    DEF  the __init__ (Self, name, Age, Hobby): 
        the self.name = name 
        self.age = Age 
        self.hobby = Hobby   # to the object space of the package properties
the Person = P1 ( ' Alex ' , ' 26 is ' , ' basketball ' )    
p1.sex = ' M '     # by 
del p1.hobby     # puncturing 
p1.age = 18 is      # change 
Print (p1.name)   # Charles 
Print (P1. the __dict__ )   # to view all the contents in the object space 
# output { 'name': 'alex' , 'age': 18, 'sex': ' F'} 
. # 1 class name + () generates an instance (object, the object space)
# 2. __init__ method is performed automatically, the image space will pass parameters Self
#. 3. attribute corresponding to the subject package

  2. Object method calls

p1.work()

II. Order inquiry

  1. Object attributes: start looking object space, and then look for the class name space, then find from the parent class

  2. The name of the class attributes: start with the class name space to find, and then find from the parent class

  3. between the object and the object is independent of the

III. Combinations

# Combination of: a property of a package to the object class, the class attribute is another object of 
class GameRole: 

    DEF  the __init__ (Self, name, AD, pH): 
        the self.name = name 
        self.ad = AD 
        self.ph = pH 

    DEF attack (Self, Role): 
        role.ph = role.ph - self.ad
         Print ( ' % s% s attack, hit% s blood, the blood left% s ' % (the self.name, Role. name, self.ad, role2.ph)) 

    DEF Weapon_attributes (Self, WEA): 
        self.wea = WEA 

class Weapon:
     DEF  the __init__(Self, name, AD): 
        the self.name = name 
        self.ad = AD 

    DEF Fight (Self, role1, role2): 
        role2.ph = role2.ph - self.ad 

        Print ( ' % s% s take attack% s,% s hit the blood, the blood left% s ' % (role1.name, the self.name, role2.name, self.ad, role2.ph))
         IF role2.ph == 0:
             Print ( ' % s dead ' % role2.name) 

role1 = GameRole ( ' Ali ' , 60, 500 ) 
role2 = GameRole ( 'Katrina ' , 80, 700 ) 
Ax = Weapon ( ' open days ax ' , 500 ) 
Broadsword = Weapon ( ' broadsword ' , 300 ) 
axe.fight (role2, role1) 
role1.Weapon_attributes (Broadsword) 
Print (role1. __dict__ ) 
role1.wea.fight (role1, role2) 
role1.wea.fight (role1, role2)

 

Guess you like

Origin www.cnblogs.com/jiujiang/p/11184351.html