What is object-oriented?
An object-oriented approach is to make a class (class) of things that have a common trait, and define methods (functions) for these traits in a class.
Specifically, for example, League of Legends. In the League of Legends we can divide all heroes into a hero class that contains the amount of blood/blue/attack/spell Strength/movement speed ... The basic properties, and we define for these heroes buy equipment, move around, get hurt blood, release skills ... Functions or methods, these basic properties and functions form a more complete class.
In fact, the class is like a template , when we create a hero Galen according to this hero class, we need to change some of the values in the template (for example, Galen does not need blue), the initial formation of the Galen's prototype. This ' hero Galen ' is based on an ' instance of the Hero class ' (Instance). Through this real Galen, we can implement the methods defined in the class, such as buying equipment and so on.
Example:
class Hero (object): hp=100
def Move (self): Print ('move!! ' ) def attack (self): print('attack ' ) Garen=hero () garen.move ()# results move!!
How do I define a class?
The example above is the way to define a class.
In Python, defining a class is through the class keyword
class class_name (object): Pass
Class names are followed by classes, and class names usually start with uppercase letters, followed by (object). The object in parentheses indicates which class the class inherits from (the class thing can inherit). Generally all classes inherit from object. Just like the init process in Linux, he is the parent of all processes. And object is the parent class for all classes.
Once you have defined the class, you can create an instance from the class.
Instance_name=class_name ()
--Let the class receive parameters:
The class we define does not receive parameters, and in practice, many classes can receive parameters .
classHero (object): HP=100#血量暂时设定为100 def __init__(SELF,MP): #mp即蓝量self.mp=MPdefMove (self):Print('%smove!!'%self) defAttack (self):Print('Attack') defprint_mp (self):Print(SELF.MP) Garen=Hero (None)
There is a __init__ function in the code above, and when we garen=Hero (None), we refer to this function. Parameters in the __INIT__ function the first must be self (the function that is created in the class must be the first argument is the "yes"), which represents the instance itself, self represents the instance itself , and self represents the instance itself !!
class Hero (object): hp=100 def __init__ (SELF,MP): self.mp=MP print(self) garen=Hero (None) <__main__. Hero Object at 0x0337f610>
Now that you know that self represents the instance itself, the one in the example above indicates that garen,self is not required to pass a value,garen=Hero (None) None was passed to MP. The self.mp=mp , in turn, binds the MP to the instance itself.
A class is a collection of many special functions, and this special refers to:
1 The first argument of each function is self, and self does not need to pass a value.
2 Each function can only be called by an instance belonging to this class, which is also a feature of object-oriented: Data encapsulation.
In addition, the functions in the class do not differ from normal functions. In addition we generally call the function of the class as the method of the class
--Let's take a look at the memory allocations for classes and instances.
classHero (object): HP=100def __init__(SELF,MP): Self.mp=MPdefMove (self):Print('move!!') defAttack (self):Print('Attack') defprint_mp (self):Print("His MP is%s"%(SELF.MP)) Garen=Hero (None) Ahri=hero (100)Print(ID (garen.mp))Print(ID (ahri.mp))#results two addresses are different14665837521466755488address of the passed parameter in memory
classHero (object): HP=100def __init__(SELF,MP): Self.mp=MPdefMove (self):Print('move!!') defAttack (self):Print('Attack') defprint_mp (self):Print("His MP is%s"%(SELF.MP)) Garen=Hero (None) Ahri=hero (100)Print(ID (GAREN.PRINT_MP))Print(ID (AHRI.PRINT_MP))#results two addresses are the same1202790412027904method in-memory address
As shown in the following:
class, and the variables are stored in their own memory. The instance simply saves its own variables and arguments, and the instance does not copy the method to its own memory address.
--Access restrictions
classHero (object): HP=100def __init__(SELF,MP): Self.mp=MPdefMove (self):Print('move!!') defAttack (self):Print('Attack') defprint_mp (self):Print("His MP is%s"%(SELF.MP))defset_mp (SELF,NEW_MP): Self.mp=New_mpgaren=Hero (None) Ahri=hero (100)
In the example above, we can modify the current hero's MP value by invoking the Set_mp method of the class.
AHRI.SET_MP (+)print(ahri.mp)77
But we can also directly use the ahri.mp=77 to modify, how can we limit the way the external changes to the instance?
If you want the instance internal variable not to be accessed externally, we can add two underscore ' __ ' before the variable
classHero (object): HP=100def __init__(SELF,MP): Self.__MP=MPdefprint_mp (self):Print("His MP is%s"% (self.__MP)) defset_mp (SELF,NEW_MP): Self.__MP=New_mpgaren=Hero (None) Ahri=hero (100)Print (AHRI.__MP)
#结果
Attributeerror: ' Hero ' object has no attribute ' __MP
At this point, when we want to modify the MP value of a hero, we can only call Hreo's Set_mp method to modify it.
The benefits of doing this:
Ensures that external code cannot arbitrarily modify the state inside the object. More robust code
We can judge the input NEW_MP value in the Set_mp method, after all, we can't let the MP value be negative.
Object-oriented Python basics