进击 of python
Object-Oriented
Alas, wide brain hurt, written yesterday, forget being given, but also to re-write! ! ! ! ! ! !
Do not force to force the hundred
If at this moment you learned object-oriented programming
I highly recommend that you do not see this blog
Because I do not want to battle with the bar fine
Familiar with my blog knows
To learn something new every time
We are introduced from the beginning of demand
Then the demand coming!
I want to play a game!
How to play?
I have a lot of dogs, I would like to have a lot of people
Dogs can bite, people can dog
Well, the design of it!
Despite a lot of talk, we should now come out to the whole dog
What attributes does a dog have?
d_name name
d_kind species
d_blod blood
d_atta attack
Well, on the whole property to determine the dog!
dog = {"d_name": "ponny",
"d_kind": "erha",
"d_blod": 100,
"d_atta": 20, }
This is a dog right
I now want two dogs
how to write?
dog = {"d_name": "ponny",
"d_kind": "erha",
"d_blod": 100,
"d_atta": 20, }
dog1 = {"d_name": "helly",
"d_kind": "laswer",
"d_blod": 100,
"d_atta": 10, }
We found finish
The two dogs too much duplicate code (key)
So make the code simple enough!
We need a way directly to put up duplicate, modify only want to change the place of
And this is just my two dogs
I'm much of a dog! ! !
Then we how to do it? ? ? ? ?
This time, it is not thought
We can use the function to do! (If the function of the front did not understand, do not look down!)
Function how to do it?
def dog(name, kind):
data = {"d_name": name,
"d_kind": kind,
"d_blod": 100,
"d_atta": 20, }
return data
This is not the function parameter passing, ah! is that OK!
The dog came out of the whole, who are not also empathy ah
def person(name, sex):
data = {"p_name": name,
"p_sex": sex,
"p_blod": 100,
"p_atta": 20, }
return data
I have a problem!
Dogs are the same? ? ? Is not it! Different categories of dogs have different sub-sub
Attack is not the same right
Then we can optimize the code!
dog_count = {"erha": 20,
"laswer": 10}
def dog(name, kind):
if kind in dog_count:
data = {"d_name": name,
"d_kind": kind,
"d_blod": 100,
"d_atta": dog_count[kind], }
else:
print("品种不明!")
return None
return data
Is there a problem? ( A problem to stop at this, look at this code is slowly, read the resume )
I thought, how? Dogs do not like people, like the? ? ? ?
Then continue to optimize the chant!
def person(name, sex):
data = {"p_name": name,
"p_sex": sex,
"p_blod": 100,
"p_atta": 20, }
if sex == "man":
data["p_atta"] = 30
return data
The above method is equivalent to two molds made two
The game starts, you have to generate a man and a dog it, how to generate it?
Function call ah! (You see, if you do not function, you can read ????)
dog1 = dog("ponny", "erha")
dog2 = dog("helly", "laswer")
person1 = person("zhangsan", "man")
I have this code for how mean?
Is not the whole out of two dogs, then put on a person? ?
There are objects, is not to start writing and the dog bites a dog person?
Function or method to write (ye, you hit it ah!)
def bite(dog_obj, person_obj):
person_obj["p_blod"] -= dog_obj["d_atta"]
print("疯狗[%s]咬了[%s],掉血[%s]..." % (dog_obj["d_name"], person_obj['p_name'], dog_obj["d_atta"]))
def beat(person_obj, dog_obj):
dog_obj["d_blod"] -= person_obj['p_atta']
print("[%s] 打了 疯狗[%s],狗掉血[%s]..." % (person_obj["p_name"], dog_obj["d_name"], person_obj["p_atta"]))
(A way to format the output)
Well, I finished it! Start playing (to determine blood <0 died before this thing is not the whole ┗ | `O '| ┛ wailing ~ ~)
dog_count = {"erha": 20,
"laswer": 10}
def dog(name, kind):
if kind in dog_count:
data = {"d_name": name,
"d_kind": kind,
"d_blod": 100,
"d_atta": dog_count[kind], }
else:
print("品种不明!")
return None
return data
def person(name, sex):
data = {"p_name": name,
"p_sex": sex,
"p_blod": 100,
"p_atta": 20, }
if sex == "man":
data["p_atta"] = 30
return data
def bite(dog_obj, person_obj):
person_obj["p_blod"] -= dog_obj["d_atta"]
print("疯狗[%s]咬了[%s],掉血[%s]..." % (dog_obj["d_name"], person_obj['p_name'], dog_obj["d_atta"]))
def beat(person_obj, dog_obj):
dog_obj["d_blod"] -= person_obj['p_atta']
print("[%s] 打了 疯狗[%s],狗掉血[%s]..." % (person_obj["p_name"], dog_obj["d_name"], person_obj["p_atta"]))
dog1 = dog("ponny", "erha")
dog2 = dog("helly", "laswer")
person1 = person("zhangsan", "man")
bite(dog2, person1)
beat(person1, dog1)
Function is simply perfect! !
But you playing playing, you will find
Bite this action (function)
Dogs should be on people's right (of course, some people are not necessarily people)
What if I go into the man?
bite(person1, dog1)
You put people only dogs can pass the method of
You find finished! Behind all wrong!
How to solve it?
Some may say, that I like to add a judge
Correct!
A method for determining a plus! (Do it yourself, ah, do come out!)
But we advanced Think!
You see ┗ | `O '| ┛ wailing ~~
The bite (bite) is not a dog (dog) action
Moreover, only a dog (dog) is!
So, we can not so!
The method bite (bite) into a dog (dog) in
This becomes, you must call the dog (dog)
Before calling bite (bite) This action (function)
def dog(name, kind):
if kind in dog_count:
data = {"d_name": name,
"d_kind": kind,
"d_blod": 100,
"d_atta": dog_count[kind], }
else:
print("品种不明!")
def bite(dog_obj, person_obj):
person_obj["p_blod"] -= dog_obj["d_atta"]
print("疯狗[%s]咬了[%s],掉血[%s]..." % (dog_obj["d_name"], person_obj['p_name'], dog_obj["d_atta"]))
return data
So although a bit mean, but we can not afford to get to the bottom
First of all, I have to call to perform right!
I should also add a bite ()
def dog(name, kind):
if kind in dog_count:
data = {"d_name": name,
"d_kind": kind,
"d_blod": 100,
"d_atta": dog_count[kind], }
else:
print("品种不明!")
def bite(dog_obj, person_obj):
person_obj["p_blod"] -= dog_obj["d_atta"]
print("疯狗[%s]咬了[%s],掉血[%s]..." % (dog_obj["d_name"], person_obj['p_name'], dog_obj["d_atta"]))
bite() # 在这呢!!!
return data
After that I added the question and again!
You violated me just start creating dog () function of early heart ah
I meant to call this function generates a dog
You are now called, let him bite!
I do not want to generate a row does not bite!
So, that being written is problematic
So we should not bite () directly into them
You should think again by the time bite () is
def dog(name, kind):
if kind in dog_count:
data = {"d_name": name,
"d_kind": kind,
"d_blod": 100,
"d_atta": dog_count[kind], }
else:
print("品种不明!")
def bite(dog_obj, person_obj):
person_obj["p_blod"] -= dog_obj["d_atta"]
print("疯狗[%s]咬了[%s],掉血[%s]..." % (dog_obj["d_name"], person_obj['p_name'], dog_obj["d_atta"]))
data["bite"] = bite ### 看这里!!!! 为了外部可以调用!!!!
return data
dog1 = dog("ponny", "erha")
print(dog1["bite"])
# <function dog.<locals>.bite at 0x051A7930>
Look, is not a function that returns if I want to call this function, you add a parenthesis on the line chant
Then we have the ultimate code optimization!
dog_count = {"erha": 20,
"laswer": 10}
def dog(name, kind):
if kind in dog_count:
data = {"d_name": name,
"d_kind": kind,
"d_blod": 100,
"d_atta": dog_count[kind], }
else:
print("品种不明!")
def bite(person_obj):
person_obj["p_blod"] -= data["d_atta"]
print("疯狗[%s]咬了[%s],掉血[%s]..." % (data["d_name"], person_obj['p_name'], data["d_atta"]))
data["bite"] = bite
return data
def person(name, sex):
data = {"p_name": name,
"p_sex": sex,
"p_blod": 100,
"p_atta": 20, }
if sex == "man":
data["p_atta"] = 30
def beat(dog_obj):
dog_obj["d_blod"] -= data['p_atta']
print("[%s] 打了 疯狗[%s],狗掉血[%s]..." % (data["p_name"], dog_obj["d_name"], data["p_atta"]))
data["beat"] = beat
return data
dog1 = dog("ponny", "erha")
dog2 = dog("helly", "laswer")
person1 = person("zhangsan", "man")
dog1["bite"](person1)
person1["beat"](dog2)
The perfect realization of the need for it! !
And when you pass in the dog to dog, it will error (see for yourself!)
To force to force so much, what use is it?
What is the relationship with the object-oriented?
In fact, you write the above code is object-oriented programming !
Your role in the design, in order to allow a role can become more solid objects
You designed a basic template, simply pass the different parameters, will have a different role
This means that you have already started to switch to the perspective of looking at things of God, God's perspective is the perspective of an object-oriented programming
God made everything in the world, he is certainly not a one made out
He must design a template species one by one, and then one by one entity bulk batches made out by mold
Made out of entities have their own characteristics, attributes, functions are different, and some of greed, lust and some people, some people are weak, some people brave
What will be the relationship between these people and who copulation, who fight and who, God's lazy tube, God only control the situation