day22
From the point of view of space research class
Where to add object properties
- You can add the class ______init______
class Human:
mind = "有思想的"
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self,argv):
print("吃饭")
sun = Human("孙宇", 18)
print(sun.__dict__)
- May be added in the process of class
class Human:
mind = "有思想的"
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self):
self.habby = "女"
print("吃饭")
sun = Human("孙宇", 18)
sun.eat()
print(sun.__dict__)
- Outside the class can also add
class Human:
mind = "有思想的"
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, argv):
print("吃饭")
sun = Human("孙宇", 18)
sun.sex = "男"
print(sun.__dict__)
Where to add the class attribute
- Inner class
class Human:
mind = "有思想的"
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, argv):
Human.body = argv
print("吃饭")
sun.eat("大脸")
print(Human.__dict__)
- Outside the class
class Human:
mind = "有思想的"
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, argv):
print("吃饭")
Human.lunguage = "中文"
print(Human.__dict__)
The relationship between object space and class space
Space and space objects class have the same name, the object is certainly first look in the object space
Find the order:
Object Name: Object class space object pointer ---> class space ---> parent space
The name of the class name: class space ---> parent space
class Human:
mind = "有思想的"
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, argv):
print("吃饭")
sun.mind = "无脑"
print(sun.mind) # 无脑
print(Human.mind) # 有思想的
The relationship between class and class
Dependencies: The main points from
The class name of a class or an object of another class incoming Method
class Elephant:
def __init__(self, name):
self.name = name
def open(self, obj):
print(f"{self.name} 默念三声:3,2,1 开门")
obj.be_open()
def close(self, obj):
print(f"{self.name} 默念三声“3,2,1 关门")
obj.be_close()
class Refrigerator:
def __init__(self, name):
self.name = name
def be_open(self):
print(f"{self.name} 冰箱 被打开了")
def be_close(self):
print(f"{self.name} 冰箱 被关闭了")
qiqi = Elephant("qiqi")
haier = Refrigerator("haier")
qiqi.open(haier) # 主.方法(从)
qiqi.close(haier)
# qiqi 默念三声:3,2,1 开门
# haier 冰箱 被打开了
# qiqi 默念三声“3,2,1 关门
# haier 冰箱 被关闭了
connection relation
And a combination of similar
class Boy:
def __init__(self, name, girlfriend=None):
self.name = name
self.girlfriend = girlfriend
def have_a_diner(self):
if self.girlfriend:
print(f"{self.name}请他的{self.girlfriend.age}岁的,{self.girlfriend.body}女朋友{self.girlfriend.name}一起吃烛光晚餐")
else:
print("吃啥吃")
class Girl:
def __init__(self, name, age, body):
self.name = name
self.age = age
self.body = body
def skill(self,obj):
print(f"{obj.name}的女朋友{self.name} 会用萝莉音直播")
yec = Boy("叶尘")
qiao = Girl("乔碧萝", 53, "小钢炮")
yec.girlfriend = qiao
yec.have_a_diner()
yec.girlfriend.skill(yec)
# qiao = Girl("乔碧萝", 53, "小钢炮")
# yec = Boy("叶尘", qiao)
# yec.have_a_diner()
# yec.girlfriend.skill(yec)
Combination of relationship
Combination: a class object is encapsulated into another attribute of the object class, called combination.
class GameRole:
def __init__(self, name, ad, hp):
self.name = name
self.ad = ad
self.hp = hp
class Weapon:
def __init__(self, name, ad):
self.name = name
self.ad = ad
def weapon_attack(self, p1, p2): # 依赖关系
p2.hp = p2.hp - self.ad
print(f"{p1.name}利用{self.name}给了{p2.name}一下子,{p2.name}掉了{self.ad}血,还剩{p2.hp}血")
gailun = GameRole("盖伦", 15, 800)
xin = GameRole("赵信", 20, 500)
sword = Weapon("大宝剑", 20)
# 盖伦利用大宝剑给赵信一下子
sword.weapon_attack(gailun, xin)
# 1、功能虽然实现了,但是逻辑上不合理,应该是人物对象调用方法.主体
# 2、游戏人物本身就应该绑定武器属性
class GameRole:
def __init__(self, name, ad, hp):
self.name = name
self.ad = ad
self.hp = hp
def weapon_swe(self, obj):
self.weapon = obj # 组合关系
class Weapon:
def __init__(self, name, ad):
self.name = name
self.ad = ad
def weapon_attack(self, p1, p2): # 依赖关系
p2.hp = p2.hp - self.ad # self永远默认接收本类实例化对象
print(f"{p1.name}利用{self.name}给了{p2.name}一下子,{p2.name}掉了{self.ad}血,还剩{p2.hp}血")
gailun = GameRole("盖伦", 15, 800)
xin = GameRole("赵信", 20, 500)
sword = Weapon("大宝剑", 20)
# 给游戏人物封装武器属性
gailun.weapon_swe(sword)
gailun.weapon.weapon_attack(gailun, xin)