1.什么是对象?什么是类?
对象是特征与技能的结合体,类是一系列对象相同的特征与技能的结合体。
2.绑定方法的有什么特点
对象的绑定方法,是由对象来调用的
特殊之处就是把对象当作第一个参数传入该方法中
3.基于面向对象设计一个对战游戏
# 仿制英雄联盟英雄角色
class Riven:
camp = 'Noxus'
def __init__(self,nickname,aggressivity=54,
life_value=414,money=1001,
armor=3):
self.nickname = nickname
self.aggressivity = aggressivity
self.life_value = life_value
self.money = money
self.armor = armor
def attack(self,enemy):
damage_value = self.aggressivity-enemy.armor
enemy.life_value-=damage_value
class Garen:
camp = 'Demacia'
def __init__(self,nickname,aggressivity=58,
life_value = 455,
money = 100,
armor = 10):
self.nickname = nickname
self.aggressivity = aggressivity
self.life_value = life_value
self.money = money
self.armor = armor
def attack(self,enemy):
damage_value=self.aggressivity-enemy.armor
enemy.life_value-=damage_value
class BlackClaver:
def __init__(self,price =475,aggrev=9,life_value=100):
self.price = price
self.aggrev = aggrev
self.life_value = life_value
def update(self,obj):
obj.money -= self.price
obj.aggressivity+=self.aggrev
obj.life_value +=self.life_value
def fire(self,obj):
obj.life_value-=1000
r1 = Riven('草丛伦')
g1 = Garen('盖文')
b1 = BlackClaver()
print(f'草丛伦攻击值:{r1.aggressivity},草丛伦生命值:{r1.life_value},草丛伦金币:{r1.money}')
if r1.money>b1.price:
r1.b1=b1
b1.update(r1)
print('草丛伦装备购买成功')
print(f'草丛伦攻击值:{r1.aggressivity},草丛伦生命值:{r1.life_value},草丛伦金币:{r1.money}')
print(f'盖文初始生命值:{g1.life_value}')
r1.attack(g1)
print(f'盖文受到普攻伤害,生命值:{g1.life_value}')
r1.b1.fire(g1)
print(f'盖文受到装备伤害,生命值:{g1.life_value}')