1.什么是对象?什么是类?
对象是特征与技能的结合体
类是一系列对象相同的特征与技能的结合体
2.绑定方法的有什么特点
由对象来调用称之为对象的绑定方法,
不同的对象调用该绑定方法,则会将不同的对象传入该绑定方法中
对象的绑定方法,是由对象来调用的,
特殊之处就是把对象当作第一个参数传入该方法中
3.基于面向对象设计一个对战游戏
class Riven:
'''放逐之刃'''
camp = 'Noxus'
def __init__(
self,
name,
aggressivity = 56, #初始攻击力
life_value = 558,
money = 1000,
armor = 24): #护甲
self.name = name
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,
name,
aggressivity = 56, #初始攻击力
life_value = 551,
money = 1000,
armor = 21): #护甲
self.name = name
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 Teemo:
'''迅捷斥候'''
camp = 'Yordels'
def __init__(
self,
name,
aggressivity = 49, #初始攻击力
life_value = 551,
money = 1000,
armor = 14): #护甲
self.name = name
self.aggressivity = aggressivity
self.life_value = life_value
self.money = money
self.armor = armor
class BlackCleaver:
def __init__(
self,
price = 475,
aggrev = 8,
life_value = 80):
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
#实例化对象
r1 = Riven('锐雯')
g1 = Garen('盖伦')
b1 = BlackCleaver()
#购买装备前属性
print(r1.aggressivity, r1.life_value, r1.money)
if r1.money > b1.price: #如果金币大于售价
r1.b1 = b1 #r1新增属性
b1.update(r1) #r1各项属性随购买b1变化
#购买装备后的属性
print(r1.aggressivity, r1.life_value, r1.money)
print('盖伦初始血量',g1.life_value)#挨打前生命值
print('锐雯初始血量',r1.life_value)#挨打前生命值
round = 1
while True:
print(f'第{round}回合')
round+=1
r1.attack(g1) # 普攻
if g1.life_value < 0:
print('盖伦被打死了')
break
else:
print('盖伦血量',g1.life_value)
g1.attack(r1) # 普攻
if r1.life_value < 0:
print('锐雯被给打死了')
break
else:
print('锐雯血量',r1.life_value)