1 # -*- coding:utf-8 -*-
2 '''
3 练习1:编写一个学生类,产生一堆学生对象, (5分钟)
4 要求:
5 有一个计数器(属性),统计总共实例了多少个对象
6 '''
7 class Student:
8 school = 'luffycity'
9 count = 0
10 def __init__(self, name, age, sex):
11 self.name = name
12 self.age = age
13 self.sex = sex
14 Student.count += 1
15
16 def learn(self):
17 print("%s is learning" % self.name)
18
19
20 stu1 = Student('alice', 18, 'female')
21 stu2 = Student('alex', 32, 'male')
22
23 # print(Student.count)
24 # print(stu1.count)
25 # print(stu2.count)
26 # print(Student.__dict__)
27 # print(stu1.__dict__)
28 # print(stu2.__dict__)
29
30 '''
31 练习2:模仿LoL定义两个英雄类, (10分钟)
32
33 要求:
34 英雄需要有昵称、攻击力、生命值等属性;
35 实例化出两个英雄对象;
36 英雄之间可以互殴,被殴打的一方掉血,血量小于0则判定为死亡。
37 '''
38 class Garen:
39 camp = 'Demacia'
40
41 def __init__(self,nickname,life_value,aggressivity):
42 self.nickname = nickname
43 self.life_value = life_value
44 self.aggressivity = aggressivity
45
46 def attack(self,enemy):
47 enemy.life_value -= self.aggressivity
48 # r1.life_value -= g1.aggressivity
49 if enemy.life_value == 0:
50 print('%s is over' % enemy.nickname)
51
52
53 class Riven:
54 camp = 'Noxus'
55
56 def __init__(self, nickname, life_value, aggressivity):
57 self.nickname = nickname
58 self.life_value = life_value
59 self.aggressivity = aggressivity
60
61 def attack(self, enemy):
62 enemy.life_value -= self.aggressivity
63
64 # g1 = Garen('草丛伦',100,30)
65 g1 = Garen('草丛伦',100,80)
66 r1 = Riven('可爱的锐雯',80,50)
67
68 print(r1.life_value)
69 g1.attack(r1)
70 print(r1.life_value)