Initial object oriented
1. Class instantiation:
The process of calling a class to generate an object is called class instantiation,
The result of instantiation is an object, or an instance
class People:
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
def run(self):
print('%s is running' % self.name)
obj = People('carter', 18, 'male')
# The namespace of the object will be generated,How to view
print(obj.__dict__)
print(obj.name)
Instantiation did three things
1. Generate an empty object first
2. Automatically trigger the execution of "init" function within the class
3, simultaneous interpreting the empty object and the parameters passed in the parentheses to __init__, and customize the unique attributes for the object.
Object addition, deletion, modification and query
obj.hobby = 'beauty' # increase
del obj.name # delete
obj.age = '20' # modify
print(obj.__dict__) # lookup
2. Search order of object attributes: first find the object's own namespace - the namespace of the class
class People:
x=1
def __init__(self,name,age,sex):
self.name=name
self.age=age
self.sex=sex
def run(self): #self=obj
print('%s is running' %self.name) #obj.name
obj=People('carter',18,'male') #People.__init__(obj,'carter',18,'male')
obj1=People('carter1',18,'male') #People.__init__(obj,'carter1',18,'male')
obj2=People('carter2',18,'male') #People.__init__(obj,'carter2',18,'male')
#1,Data properties of class:It is used for objects and directly shared for all objects. The memory address is the same
print(People.x)
People.x=11111
print(id(People.x),People.x)
obj.x='obj================>'
print(id(obj.x),obj.x)
print(id(obj1.x),obj1.x)
print(id(obj2.x),obj2.x)
# 2,Function attribute of class: it is also used for objects, but it is different to bind to different objects
# The binding method and memory address are different, but they just want to be the same function
print(People.run) # <function People.run at 0x00000226E7C78A60>
People.run(123123)
People.run(obj)
print(obj.run)
print(obj1.run)
print(obj2.run)
3. The particularity of binding method:
1. Whoever binds to it should call it,
2. Whoever calls will pass in the first parameter
class People:
x=1
def __init__(self,name,age,sex):
self.name=name
self.age=age
self.sex=sex
def run(self): #self=obj
print('%s is running' %self.name) #obj.name
def f1():
print('from f1')
def f2(self):
pass
obj=People('carter',18,'male') #People.__init__(obj,'carter',18,'male')
obj.run()
obj1.run()
obj2.run()
print(People.f1)
People.f1()
print(obj.f1)
obj.f1() #People.f1(obj)
Small exercise

class People:
def __init__(self, name, aggressiveness, life_value):
self.name = name
self.aggressiveness = aggressiveness
self.life_value = life_value
def bite(self, enemy):
enemy.life_value -= self.aggressiveness
print("""
[%s]A bite.[%s]
//Dog lo s t blood
//Dog's blood remaining: [% s]
""" % (self.name, enemy.name, self.aggressiveness, enemy.life_value))
class Dog:
def __init__(self, name, aggressiveness, life_value):
self.name = name
self.aggressiveness = aggressiveness
self.life_value = life_value
def bite(self, enemy):
enemy.life_value -= self.aggressiveness
print("""
[%s]One bite.[%s]
//Human blood loss: [% s]
//People's remaining blood volume: [% s]
""" % (self.name, enemy.name, self.aggressiveness, enemy.life_value))
p1 = People('people', 60, 100)
d1 = Dog('Dog', 80, 200)
p1.bite(d1)
d1.bite(p1)
Dog Wars