Namespace class object instance namespace
Creating a class creates a class name space, used to store all the names defined in the class, these names are called properties of the class
The class has two attributes: static and dynamic attributes
- Static property is defined directly in the class variables
- The method of dynamic properties is defined in class
Wherein the data attribute class are shared to all objects
>>>id(egg.role) 4341594072 >>>id(Person.role) 4341594072
The dynamic properties of the class is bound to all objects
>>>egg.attack <bound method Person.attack of <__main__.Person object at 0x101285860>> >>>Person.attack <function Person.attack at 0x10127abf8>
Create an object / instance creates a space for the name of the object / instance, the name of the object store / instance, known properties of the object / instance
In obj.name will start obj own name space to find the name, can not find the class to find, can not find the class to find the parent can not be found ... The last throw an exception
Use a combination of object-oriented
An important way to reuse software in addition to inheriting There is another way, namely: Combination
Means a combination, in one class to another class of the object as a data property called composite class
Weapon class: DEF Prick (Self, obj): # This is the equipment of active skills, killed the other obj.life_value - = 500 # assume that the attack is 500 class the Person: # definition of a human role = 'person' # people character attributes are people DEF __init __ (Self, name): self.name name = # each character has its own nickname; self.weapon = weapon () # bind to the role of a weapon; Egg = the person ( 'Egon' ) egg.weapon.prick () #egg combines a weapon object may directly be used in all methods egg.weapon combinations class
Ring is composed of two circular, ring area is the area outside circle minus the area inside the circle. It is the internal circumference of the ring plus the circumference of a circle the circumference of the outer circle.
This time, we will achieve a first round class, calculate perimeter and area of a circle. Examples of a combination of circular and in the "ring-based" as their attribute with
Math PI Import from class Circle: '' ' defines a circular class; providing calculate the area (area) and a method circumference (Perimeter) a ' '' DEF the __init __ (Self, RADIUS): self.radius RADIUS = DEF Area ( Self): return * PI * self.radius self.radius DEF Perimeter (Self): return 2 * PI * self.radius circle circle = (10) # instantiate a circle area1 = circle.area () # circular area is calculated per1 = circle.perimeter () # circumference calculated print (area1, per1) # print area and perimeter of circle class Ring: '' ' defines a class ring provided the ring area and the perimeter of the methods ' '' DEF the __init__ (self, radius_outside, radius_inside): Circle = self.outsid_circle (radius_outside) self.inside_circle = Circle (radius_inside) DEF Area (Self): return self.outsid_circle.area () - self.inside_circle.area () DEF Perimeter (Self): return self.outsid_circle.perimeter () + self.inside_circle.perimeter () ring ring = (10,5) # instantiate an annular print (ring.perimeter ()) # annular perimeter calculated print (ring.area ()) # annular area calculated
In combination with the established relationship between class and class combination, it is a 'have' relationships, such as birthday professors, professors teach courses python
class BirthDate:
def __init__(self,year,month,day): self.year=year self.month=month self.day=day class Couse: def __init__(self,name,price,period): self.name=name self.price=price self.period=period class Teacher: def __init__(self,name,gender,birth,course):
self.name=name
self.gender=gender
self.birth=birth
self.course=course
def teach(self):
print('teaching')
p1=Teacher('egon','male',
BirthDate('1995','1','27'),
Couse('python','28000','4 months')
)
print(p1.birth.year,p1.birth.month,p1.birth.day)
print(p1.course.name,p1.course.price,p1.course.period)
'''
运行结果:
1 27
python 28000 4 months
'''
When significantly different between the classes, and the smaller is the component of a larger class of the class required, in combination with better
Acquaintance Object-Oriented Summary
The definition of a human
class Person: # definition of a human role = 'person' # character attributes are people who DEF __init __ (Self, name, aggressivity, life_value, Money): self.name name = # Each character has its own nickname; Self .aggressivity = aggressivity # each character has its own attack; self.life_value = life_value # each character has its own value of life; self.money = Money DEF attack (Self, dog): # of people can attack dogs, here the dog is also an object. # Attacked the dog, then the dog's health will be based on people's attack power and decrease
dog.life_value - = self.aggressivity
The definition of a dog
class Dog: # define a dog role = 'dog' # dog character attributes are dog DEF __init __ (Self, name, Breed, aggressivity, life_value): self.name = # name each dog has its own nickname ; self.breed = breed # each dog has its own varieties; self.aggressivity = aggressivity # each dog has its own attack; self.life_value = life_value # each dog has its own value of life ; DEF bite (Self, people): # dog can bite, where the dog is also an object. # Dog bites a man, then the value of human life will decline According attack dog people.life_value - = self.aggressivity
Next, we created a new class of weapons.
Weapon class: DEF the __init __ (Self, name,. price, aggrev, life_value): the self.name name = self.price =. price self.aggrev = aggrev self.life_value = life_value DEF Update (Self, obj): #obj is to tape people in this equipment obj.money - = self.price # use this weapon so people spend money corresponding money to reduce obj.aggressivity + = self.aggrev # people bring this equipment can increase the attack obj.life_value + = self.life_value # people bring this equipment can increase the value of life def prick (self, obj): # this is the equipment of active skills, killed the other obj.life_value - = 500 # 500 is the assumption that the attack
Interactive test
lance = Weapon ( 'spear', 200,6,100) Egg = the Person ( 'Egon', 10,1000,600) # create a real person Egg HA2 = Dog ( 'Erleng child', 'Huskies' 10 , 1000) # creates a real dog HA2 #egg alone in the battle, "Erleng child was" deeply difficult decision poor life savings to buy a weapon if egg.money> lance.price: # If the egg money than equipment more than the price, you can buy a spear lance.update (egg) #egg spend money to buy a spear self-defense, and the property itself has been improved egg.weapon = lance #egg equipment on the spear print (egg.money, egg.life_value, egg.aggressivity) print (ha2.life_value) egg.attack (ha2) #egg hit ha2 about print (ha2.life_value) egg.weapon.prick (ha2) # weapons and skills to launch print (ha2.life_value) # ha2 lost to the cunning human use weapons to win, blood trough half empty
According to this line of thinking a little bit of design classes and objects, eventually you can achieve a arcade game.