An object-oriented acquaintance:
In addition to object-oriented, there is a process-oriented, process-oriented and that what is it?
Process-oriented:
Is a process-oriented programming ideas, is to streamline the code, and its core is the process, that is, steps to resolve the problem, namely what to do first what to do.
Pros: The complex issue of the process, simplified
Disadvantages: Extensibility low, low reusability, low maintenance
Object-oriented:
It is also a programming idea, its core is the target word, no longer tangled the process, to get the desired design object.
Advantages: high scalability, high reusability, maintainability high
Disadvantages: higher to solve the problem of complexity
Scenario: Object Oriented should be applied directly to the customer, such as requiring high flexibility qq, micro-channel,
The process should be applied for does not change often, such as linux kernel
Classes and Objects:
Object: that has certain characteristics and behavior of the combination, it is exactly the existence of
Class: is a series combination of an object having the same characteristics and behavior, an abstract entity
In life there is a first, and then the object classes, and in programming is the first object classes in there.
In the feature category it is arguably the properties, behavior is a function of
The class name must play to see to know the name of Italy, and the use of large hump of writing, the first letter capitalized
example:
class Person:
pass
Create Object
p = Person()
print(p) # <__main__.Person object at 0x10b11d588>
Attribute manipulation of objects
We want to increase the property may, after creating an object, use dot syntax variable name.
p.name = 'lw'
print (p.name) # get the same dot syntax is
Find the order of objects
Go first to the object itself, and then find the class object is located.
All the properties of the current object print (Object .__ dict__) access
Currently resides class object print (Object .__ class__) access to information
__init__ initialization method:
This is an initialization method, is essentially a function.
Its characteristics are:
1. automatically executed when it generates an object class is instantiated
2. Object and will automatically passed as the first argument
3. This function has no return value
Function: the initial value is given to the object
class Dog: def __init__(self,kind,color,age): self.kind = kind self.color = color self.age = age d1 = Dog("二哈","黑白",1) d1 = Dog("泰迪","棕色",2)
The essence of the object function is to integrate data and process the data together, so that the object can not only get to deal with the data processing function can get data
Binding method:
Binding method can also be called binding function
Inside binding method is divided into:
Object Binding Methods:
Features: 1 when the object will call the function, automatic object as the first argument
2. When invoked with the class name is a common function, there are several parameters must pass several parameters
Class binding method:
@classmethod
When the object will call the function, automatically class as the first argument
Non-binding approach
@staticmethod
Neither data nor the data object needs class, just an ordinary function
Class examples:
Import Random Import Time class Hero: DEF __init __ (Self, name, Level, Blood, ATT, q_hurt, w_hurt, e_hurt): # simple written LCS = about locals () lcs.pop ( " Self " ) Self .__ dict __ Update (LCS. ) DEF attack (Self, enemy): enemy.blood - = self.att Print ( " % s% s release of a normal attack caused damage to enemies% s of remaining blood volume% s " % (self.name, enemy. name, self.att, enemy.blood)) IF enemy.blood <= 0 : Print ( " % s% s was used to kill the normal attack "% (Enemy.name, self.name)) DEF Q (Self, Enemy): enemy.blood - = self.q_hurt Print ( " % s% s release of the Q caused damage enemies remaining% s% s blood " % (the self.name, enemy.name, self.q_hurt, enemy.blood)) IF enemy.blood <= 0 : Print ( " % S Q are used to kill the skills S% " % (enemy.name, Self .name)) DEF W (Self, enemy): enemy.blood - = self.w_hurt Print ( " % s% s release of the W% s causing damage enemies Health remaining% s " % (self.name, enemy.name, self.w_hurt, enemy.blood)) IF enemy.blood <= 0 : Print ( " % S is used to kill the skills W S% " % (enemy.name, the self.name)) DEF E (Self, Enemy): enemy.blood - = self.e_hurt Print ( " % s% s of release% s E causing blood damage remaining enemy% s " % (the self.name, enemy.name, self.e_hurt, enemy.blood)) IF enemy.blood <= 0 : Print ( " % S% s E is used skill kill " % (enemy.name, the self.name)) h1 of = Hero ( " Yasuo " , 20 , 2000 ,100 , 600 , 0 , 1000 ) H2 = Hero ( " daji " , 20 is , 2000 , 100 , 600 , 500 , 1000 ) H3 = Hero ( " Luban " , 20 is , 1500 , 700 , 100 , 200 is , 300 ) H4 = Hero ( " Cai Wenji " , 20 , 2000 , 10 ,0 , 0 , 10 ) # # h1.attack (H2) # h2.Q (h1 of) # h2.E (h1 of) # h2.W (h1 of) # from the dictionary come up with a random value def random_hero (heros): hero_index = the random.randint ( . 1 , len (Heros)) return Heros [hero_index] the while True: # # all loaded into the dictionary attacks to remove a random funcs = { . 1 : Hero.Q, 2 : Hero.W , . 3 : Hero.E, . 4 : Hero.attack} func_index = the random.randint ( . 1 , . 4 ) FUNC =funcs [func_index] # all heroes methods loaded into the dictionary in order to randomize remove a Heros = { . 1 : h1 of, 2 : H2, . 3 : H3, . 4 : H4} Hero = random_hero (Heros) # remaining heroes other_heros = } { NEW_INDEX = . 1 for K, V in heros.items (): IF V =! hero: other_heros [NEW_INDEX] = V NEW_INDEX + = . 1 # randomly picked from a hero to the rest of the hero beaten Enemy = random_hero (other_heros ) # hit him func(hero, enemy) if enemy.blood <= 0: break time.sleep(0.5)