练习:编写一个学生类,产生一堆学生对象
要求:有一个计数器(属性),统计总共实力化多少个对象
class Student:
school = 'luffycity'
count = 0
def __init__(self, name, sex, age): # 独有特征
self.Name = name
self.Sex = sex
self.Age = age
Student.count += 1 # 核心,这样是累加调用的次数;self.count 是对象自己一个 值为1
def learn(self): # 技能
print('is learning')
stu1 = Student("alex",'male',28)
stu2 = Student("egon",'male',18)
stu3 = Student("wusi",'male',27)
print(stu1.count)
print(stu2.count)
print(stu3.count)
练习2:模仿LOL定义两个英雄类,
要求:
英雄有昵称,攻击力,生命值等属性;
实例化两个英雄对象
英雄之间可以互殴,被殴掉一方的掉血,血量小于0则判断死亡。
class Garen:
camp = 'camp'
def __init__(self,nickname,life_value,aggresivity):
self.nickname=nickname
self.life_value=life_value
self.aggresivity=aggresivity
def attack(self,enemy):
enemy.life_value -= self.aggresivity
class Riven:
camp = 'Noxus'
def __init__(self,nickname,life_value,aggresivity):
self.nickname=nickname
self.life_value=life_value
self.aggresivity=aggresivity
def attack(self,enemy):
enemy.life_value -= self.aggresivity
# if enemy.life_value <= 0:
g1=Garen('草丛伦',100,30)
r1=Riven('芮雯雯',80, 50)
print(r1.life_value)
g1.attack(r1)
g1.attack(r1)
g1.attack(r1)
print(g1.attack(r1))
print(r1.life_value)