Object oriented
First, the concept of image-oriented
the key to the object-oriented concept is the object. In contrast to process-oriented, process-oriented is the problem, and the way to solve the problem is to follow the steps step by step list. Object-oriented, however, is to create objects and then interact between objects. For objects and not for procedures. So what is an object? An object is a combination of a feature (static property) and a skill (dynamic property). such as the characters in the game, there are skin, name and other attributes, there are various attack skills and so on.
Advantages: It solves the extensibility of the program.
Disadvantages: 1, increase the complexity, 2, can not accurately predict the processing process and results
Second, class and object
2.1 Examples of classes and objects
classDog:role='Dog'counter=0def __init__(SELF,NAME,AGE,KIND,ATC,HD): Self.name=name Self.age=Age Self.kind=Kind Self.hd=HD SELF.ATC=ATCdefDog_func (self,pers): Pers.hd-=SELF.ATCPrint('%s bite%s,%s HD lost%s still has%s'%(SELF.NAME,PERS.NAME,PERS.NAME,SELF.ATC,PERS.HD))##注意:#1. There can be any Python code in the class that executes during the class definition phase#2. Thus a new namespace is generated, which is used to hold the class's variable name and function name, which can be viewed by dog.__dict__#3. For classic classes We can manipulate the name of the class namespace through the dictionary (there are restrictions on the new class), but Python provides us with a special. Syntax#4. The point is the syntax for accessing the property, and the name defined in the class is the property of the class#use of classes in programs#.: specifically used to access attributes, the essence of the operation is __dict__Dog.role#equals the operation of the classic class dog.__dict__[' name ']dog.role='Oldboy' #equals the operation of the classic class dog.__dict__[' name ']= ' Oldboy 'Dog.counter=1#equals the operation of the classic class dog.__dict__[' age ']=1delDog.counter#equals the operation of the classic class Dog.__dict__.pop (' Conuter ')#objects in the program#call class, or instantiation, to get an object#D1=dog ()#D2=dog ()#D3=dog ()#So, D1, D2, D3 are all the same, and these three have different properties besides similar attributes, which is used in __init__#Note: This method is executed after the object has been generated and is used only to initialize the object and can have arbitrary code, but must not have a return valueD1=dog ('Dog1', 18,'two ha', 1000,2000)#call the class first to produce the empty object S1, and then call dog.__init__ (' Dog1 ', 18, ' two ha ', 1000,2000)D2=dog ('dog2', 18,'Golden Hair', 800,2600) D3=dog ('Dog3', 18,'Corgi', 600,1800)#use of objects in programs#execute __init__,s1.name= ' Bull grenade ', and it will obviously produce the object's namespaceD2.__dict__{'name':'dog2',' Age': 18,'Kind':'Golden Hair','HD': 2600,'ATC': 800}d2.name#s2.__dict__[' name ']D2.name='dog222' #s2.__dict__[' name ']= ' Wang San Cannon 'D2.course='python' #s2.__dict__[' course ']= ' python 'delD2.course#S2.__dict__.pop (' Course ')#in the program: Define the class first, then produce the object
Special properties built into the class in 2.2python
#Python special properties built into the classThe class name.__name__#the name of the class (string)The class name.__doc__#the document string for the classThe class name.__base__#the first parent class of a class (speaking of inheritance)The class name.__bases__#a tuple of all the parent classes of the class (speaking of inheritance)The class name.__dict__#Dictionary properties of the classThe class name.__module__#module where the class definition residesThe class name.__class__#class for instance (in modern class only)
2.3 Object (obj) to find the order of attributes
In the Obj.name will first find the name in Obj's own namespace, can not find the class to find, the class can not find the parent class ... Throw an exception if you can't find it at the end
Iii. Inheritance and derivation
Inheritance is a way to create a new class, a new class created in Python can inherit one or more parent classes, the parent class becomes a base class or a superclass, and a new class is called a derived class or subclass
In the process of developing a program, if we define a Class A and then want to create another class B, but most of the content of Class B is the same as Class A, it is not possible to write a class B from scratch, which uses the concept of class inheritance. To create a new class B through inheritance, let B inherit all of the attributes (data attributes and function attributes) of a, a, a, to implement code reuse.
Not only can reuse their own classes, but also inherit others, such as the standard library, to customize the new data types, which is greatly shorten the software development cycle, for large-scale software development, significant.
Note: A property reference such as G1.life_value will first find the Life_value from the instance and then go to the class and look for it in the parent class ... Until the top-most parent class.
class # defining the parent class Pass class # defining the parent class Pass class # single inheritance, base class is ParentClass1, derived class is subclass Pass class # Python supports multiple inheritance, separating multiple inherited classes with commas Pass
3.1 Viewing inheritance
>>> subclass1.__bases__ #__base__只查看从左到右继承的第一个子类, __bases__ is to view all inherited parent classes (<class ' __main__. ParentClass1 ',,) >>> subclass2.__bases__ (<class ' __main__. ParentClass1 ';, <class ' __main__. ParentClass2 ' >)
3.2 Classic class and New class
1). Only in the Python2 want $ The new class and the classic class, the unification in the Python3 is the new class
2). In Python2, there is no explicit class that inherits the object class, and the subclass of the class, which is the classic class
3). In Python2, the class that explicitly declares the inheriting object, and the subclasses of that class, are all modern classes
3). In Python3, whether or not to inherit object, the object is inherited by default, that is, all classes in Python3 are modern classes
PS If you do not specify a base class, the Python class inherits the object class by default, object is the base class for all Python classes, and it provides implementations of some common methods, such as __str__.
>>> parentclass1.__bases__ (<class ' object ';,) >>> parentclass2.__bases__ (<class ' object ') ,)
classHero:def __init__(self,nickname,aggressivity,life_value): Self.nickname=Nickname Self.aggressivity=aggressivity Self.life_value=Life_valuedefMove_forward (self):Print('%s move forward'%self.nickname)defMove_backward (self):Print('%s Move Backward'%self.nickname)defMove_left (self):Print('%s move forward'%self.nickname)defmove_right (self):Print('%s move forward'%self.nickname)defAttack (Self,enemy): Enemy.life_value-=self.aggressivityclassGaren (Hero):PassclassRiven (Hero):PassG1=garen ('Bush LUN', 100,300) R1=riven (' Wen Wen', 57,200)Print(G1.life_value) r1.attack (G1)Print(G1.life_value)" "Run Results" "
3.3 Derivation
Of course, subclasses can also add their own new properties or redefine them here (without affecting the parent class), and it is important to note that once you have redefined your own properties and have the same name as the parent, you will be able to invoke the new attributes when you call them.
In subclasses, the new function attribute of the same name, when editing functions within the function, it is possible to reuse the same function function of the parent class, it should be called the normal function, that is: class names. Func (), this is the same as calling the normal function, so even if the self parameter to pass the value
classRiven (Hero): Camp='Noxus' def __init__(Self,nickname,aggressivity,life_value,skin): Hero.__init__(Self,nickname,aggressivity,life_value)#calling the parent class featureSelf.skin=skin#New Properties defAttack (Self,enemy):#define the new attack on your own, no longer use the parent class's attack, and do not affect the parent classHero.attack (Self,enemy)#Invoke function Print('From Riven') defFly (self):#define the new in your own Print('%s is flying'%self.nickname) R1=riven (' Wen Wen', 57,200,'Bikini') r1.fly ()Print(R1.skin)" "running results Rui Wen is flying bikini" "
Four, the combination
The important way of software reuse in addition to inheritance there is another way, namely: the combination
A combination refers to a class in which an object of another class is used as a data property, called a combination of classes
>>> class Equip: #武器装备类 ... def fire (self): ... Print (' Release fire skill ') ... >>> class Riven: #英雄Riven的类, a hero needs to be equipped and therefore needs to be combined equip class ... camp= ' Noxus ' ... def __init__ (self,nickname): ... self.nickname=nickname ... Self.equip=equip () #用Equip类产生一个装备, assign the equip property to the instance ... >>> r1=riven (' Rui Wen ') >>> R1.equip.fire () # You can use the method that the combined class produces to hold the object in the release fire skill
Seventh day of Python learning