First, what is object-oriented
1. Object-oriented programming (Oop,object oriented programming)
Three main features of OOP: Data encapsulation, inheritance and derivation, polymorphism.
All the programs are composed of a certain property and the behavior of the object, the access of different objects through the function call to complete, all communication between objects through the method call, through the encapsulation object data, improve the reuse rate.
If you have any questions, please be positive to point out.
Ii. Creating classes and objects
Class Role (object): #object是父类, inherits the parent class Def __init__ (Self,name,role,weapon,life_value): #函数在class里叫做方法 self. Name = Name #成员变量, self represents the name passed in, this variable can be used throughout the class, and a local variable is changed to a global variable self.role = role Self.weapon = weapon Self.life_val = Life_value def buy_weapon (self,weapon): Print ("[%s] is buying [%s]"% (self.name,weapon)) Self.weapon = weapon # To turn an abstract class into a concrete process called instantiation P1 = Role ("Tom", ' Police ', ' Desert Eagle ', "a") #Role (P1, "Tom", ' Police ', ' D Esert Eagle ', max. T1 = Role ("Stanley", ' Terrorist ', ' Glock ', ' + ') #Role (T1, "Stanley", ' Terrorist ', ' B11 ', ") print (" T1 ' s Weapon was [%s] before "% t1.weapon) print (" = ". Center (+, ' = ')) t1.buy_weapon (' AK47 ') #转换为Role. Buy_weapon (T1, ' AK47 ') Print ("T1 ' s weapon is [%s] now"% t1.weapon) Result:t1 ' s weapon was [Glock] before======================================= =[stanley] is buying [Ak47]t1 's weapon is [AK47] Now
This code is the basic idea of object-oriented programming, where class is a keyword that defines a class whose name is role, which inherits the Python parent class, named object. There are many definitions of functions under the class, but in class, the function is called a method, so for the time being there is a __init__ and Buy_weapon method underneath a named role class.
The first method __init__ is the initialization of the constructor method, the first parameter of the constructor method is always self, representing the object of the class itself, when the object is actually constructed, the parameter does not have to write, the Python compiler will add itself, the function of the constructor is to assign values to the As the name passed in below is assigned to Self.name, this is done because in class, each method is still to follow the function rules, each variable under the function is a local variable, and in other functions it is not able to call each other, so it changes a local variable into a global variable.
The second method, in addition to the self parameter that the compiler adds itself, receives the passed-in variable and prints the arguments of the weapon parameter. After print, the meaning of Self.name is to assign the name of the caller to self.
The next step is to instantiate P1 and T1, which is to turn an abstract class into a concrete process that generates P1 and T1 two characters. T1 = Role ("Stanley", ' Terrorist ', ' Glock '), the Python interpreter converts this to role (T1, "Stanley", ' Terrorist ', ' B11 ', 100), The T1 converted by the interpreter here is actually self.
Now that the T1 character is buying, T1.buy_weapon (' AK47 '), the Python interpreter converts this to role.buy_weapon (T1, ' AK47 '), as mentioned above, and T1 is self at this point, and can be understood as T1 incoming __init_ _ Initialize the constructor method, you can directly self.name (equivalent to t1.name) get to the name of the variable (Stanley).
Three, Package
Iv. inheritance
V. Polymorphism
Python Object-oriented programming