-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.py
391 lines (311 loc) · 11.2 KB
/
class.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#成员变量,成员函数,初始化
# class Footman:
# def __init__(self, name, health, power, armor, ft_ratio = 0):
# self.name = name
# self.health = health
# self.power = power
# self.armor = armor
# self.fantan = ft_ratio
# def print_health(self):
# print(f"{self.name}'s health is {self.health}")
# def attack(self, enermy):
# damage = max(0, self.power-enermy.armor)
# enermy.health -= damage
# self.health -= enermy.fantan * damage
# enermy.print_health()
# self.print_health()
# a = Footman("a", 100, 10, 5)
# b = Footman("b", 50, 20, 1, 0.5)
# a.print_health()
# b.print_health()
# a.attack(b)
# class Knight:
# def __init__(self, name, health, power, armor, ft_ratio = 0.4) -> None:
# self.name = name
# self.health = health
# self.power = power
# self.armor = armor
# self.fantan = ft_ratio
# def print_health(self):
# print(f"{self.name}'s health is {self.health}")
# def attack(self, enermy):
# damage = max(0, self.power-enermy.armor)
# enermy.health -= damage
# self.health -= enermy.fantan * damage
# enermy.print_health()
# self.print_health()
# def hard_attack(self, enermy):
# damage = max(0, 2*(self.power-enermy.armor))
# enermy.health -= damage
# print(f"{self.name} hard attack {enermy.name}, cause {damage} damage, {enermy.name} left {enermy.health} health")
# a = Knight("a", 100, 10, 5)
# b = Footman(name="b", health=50, power=20, armor=1, ft_ratio=0.2)
# a.print_health()
# b.print_health()
# # b.attack(a)
# a.hard_attack(b)
# class Soldier:
# def __init__(self, name, health, power, armor) -> None:
# self.name = name
# self.health = health
# self.power = power
# self.armor = armor
# def __str__(self):
# '''
# 自我介绍
# '''
# return f"{self.name}, has {self.health} health, {self.power} power, {self.armor} armor"
# def attack(self, enermy):
# damage = max(0, self.power-enermy.armor)
# enermy.health -= damage
# if hasattr(enermy, 'rebound'):
# self.health -= enermy.rebound * damage
# class Footman(Soldier):
# def __init__(self, name, health, power, armor, rebound=0.2) -> None:
# super().__init__(name, health, power, armor) #初始化父类
# self.state = 'offense'
# self.rebound = rebound
# def defense_state(self):
# if self.state=='offense':
# self.armor *= 2
# self.power /= 2
# self.state = 'defense'
# def __str__(self):
# return super().__str__() + f", {self.state} state"
# def offense_state(self):
# if self.state=='defense':
# self.armor /= 2
# self.power *= 2
# self.state = 'offense'
# class Knight(Soldier):
# def __init__(self, name, health, power, armor, weapon) -> None:
# super().__init__(name, health, power, armor)
# self.__weapon = weapon
# self.weapon_damage = {"arrow": 5, "lance": 10}
# @property
# def get_weapon(self):
# return self.__weapon
# def attack(self, enermy):
# damage = max(0, self.power + self.weapon_damage[self.__weapon] - enermy.armor)
# enermy.health -= damage
# def __str__(self):
# return super().__str__() + f", use {self.__weapon}"
# # 类变量和类函数
# class Wizzard(Soldier):
# count = 0 # 静态变量(类变量)
# def __init__(self, name, health, power, armor, mega = 10) -> None:
# if Wizzard.count >=2:
# print("error")
# raise TypeError("too many wizzard")
# super().__init__(name, health, power, armor) # 初始化父类
# self.mega = mega
# Wizzard.count += 1
# self.id = Wizzard.count
# def attack(self, enermy,use_mega = 0):
# if use_mega <= self.mega :
# self.mega -= use_mega
# damage = max(0, self.power*(1 + 0.1*use_mega) - enermy.armor)
# enermy.health -= damage
# else:
# print("Not enough mega!")
# def __str__(self):
# return f"No.{self.id} wizzard, " + super().__str__() + f", remain {self.mega} mega"
# @classmethod #静态方法(类方法)
# def build_powerful_wizzard(cls, name, health, power, armor, mega):
# return cls(name, health, power, armor, mega*10)
# bob = Footman(name='bob', health=50, power=10, armor=1, rebound=0.1)
# kevin = Knight("kelvin", 100, 10, 2, weapon='lance')
# lina = Wizzard("lina", health=45, power=12, armor=1)
# lion = Wizzard("lion", health=30, power=20, armor=1, mega=15)
# bane = Wizzard.build_powerful_wizzard("bane", health=30, power=20, armor=1, mega=15)
# print(lina, lion, bane, sep='\n')
# print(kevin.__weapon)
# print(kevin._Knight__weapon)
# print(kevin.get_weapon)
# print(bob, kevin, sep='\n')
# bob.attack(kevin)
# kevin.attack(bob)
# print(bob, kevin, sep='\n')
# lina.attack(bob,6)
# lina.attack(kevin,5)
# lina.attack(kevin,4)
# lina.attack(bob)
# print(bob)
# print(kevin)
# print(lina)
# 共有变量,保护变量(_),私有变量(__)
from abc import ABC, abstractmethod
from typing import Any
class Soldier(ABC):
def __init__(self, name, health, power, armor) -> None:
self.name = name
self.health = health
self.power = power
self.armor = armor
def __str__(self):
'''
自我介绍
'''
return f"{self.name}, has {self.health} health, {self.power} power, {self.armor} armor"
@abstractmethod
def attack(self, enermy):
pass
class Footman(Soldier):
def __init__(self, name, health, power, armor, rebound=0.2) -> None:
super().__init__(name, health, power, armor) #初始化父类
self.state = 'offense'
self.rebound = rebound
def defense_state(self):
if self.state=='offense':
self.armor *= 2
self.power /= 2
self.state = 'defense'
def __str__(self):
return super().__str__() + f", {self.state} state"
def offense_state(self):
if self.state=='defense':
self.armor /= 2
self.power *= 2
self.state = 'offense'
def attack(self, enermy):
return super().attack(enermy)
class Knight(Soldier):
def __init__(self, name, health, power, armor, weapon) -> None:
super().__init__(name, health, power, armor)
self.__weapon = weapon
self.weapon_damage = {"arrow": 5, "lance": 10}
@property
def get_weapon(self):
return self.__weapon
def attack(self, enermy):
damage = max(0, self.power + self.weapon_damage[self.__weapon] - enermy.armor)
enermy.health -= damage
def __str__(self):
return super().__str__() + f", use {self.__weapon}"
# bob = Footman(name='bob', health=50, power=10, armor=1, rebound=0.1)
# kevin = Knight("kelvin", 100, 10, 2, weapon='lance')
CONSTANT_NUM = 10
soldier_name = "bob"
#SoldierCommandor
#soldierCommandor
class Test:
def __init__(self, x) -> None:
self.x = x
def __str__(self):
return f"My id:{self.x}, str"
def __repr__(self) -> str:
return f"My id:{self.x}, repr"
def __len__(self):
return self.x + 1
def __call__(self, y):
return self.x + y
def __getitem__(self, index):
if type(index)==str:
return str(self.x)+index
return self.x + index
def __index__(self):
return self.x+2
def __lt__(self, other):
return self.x < other.x
def __gt__(self, other):
return self.x > other.x
def __eq__(self, other):
return self.x==other.x-10
def __add__(self, other):
return Test(self.x+other.x)
def __mul__(self, other):
if type(other)==int:
return Test(self.x*other)
return Test(self.x**2+other.x**2)
def __matmul__(self, other):
return self.x + other.x + 1
test = Test(10)
#print(str(test))
#print(len(test))
#print(test(20)) # test.__call__(20)
# print(test[3]) # test.__getitem__(3)
# print(test["asd"]) # test.__getitem__("asd")
# c = [i for i in range(20)]
# print(c[test])
test2 = Test(20)
# print(test < test2) # test.__lt__(test2)
# test3 = Test(2)
# a = [test, test2, test3]
# print(sorted(a))
#print(test==test2)
#print(test+test2)
#print(test*test2)
#print(test@test2)
#print(test*3)
# class Soldier:
# def __init__(self, power, armor) -> None:
# self.power = power
# self.armor = armor
# def __lt__(self, other):
# if self.power + self.armor < other.power + other.armor:
# return True
# else:
# return False
# # def __str__(self) -> str:
# # return f"{self.power + self.armor}"
# def __repr__(self) -> str:
# return f"{self.power + self.armor:.3f}"
# import numpy as np
# def random_number():
# return np.random.rand(1).item()*10
# army = [Soldier(random_number(), random_number()) for _ in range(5)]
# print(army)
# print(sorted(army))
# 算法有问题
# class Soldier:
# def __init__(self, power, armor) -> None:
# self.power = power
# self.armor = armor
# def __lt__(self, other):
# if self.power < other.armor :
# return True
# else:
# return False
# def __repr__(self) -> str:
# return f"({self.power:.3f}, {self.armor:.3f})"
# import numpy as np
# def random_number():
# return np.random.rand(1).item()*10
# army = [Soldier(random_number(), random_number()) for _ in range(5)]
# print(army)
# print(sorted(army))
class A:
pass
class Soldier:
def __init__(self, power, armor) -> None:
self.power = power
self.armor = armor
def __lt__(self, other):
if self.power + self.armor < other.power + other.armor:
return True
else:
return False
# def __str__(self) -> str:
# return f"{self.power + self.armor}"
def __repr__(self) -> str:
return f"{self.power + self.armor:.3f}"
def __getattr__(self, name):
return "do not have " + name
def __getattribute__(self, name: str) -> Any:
#return super.__dict__[name]
print("get into getattribution")
return super().__getattribute__(name)
#return "have " + __name
def __setattr__(self, name, value):
print(f"set attr {name}, value {value}!")
self.__dict__[name] = value
soldier1 = Soldier(10, 20)
# print(dir(soldier1))
# print(soldier1.__dict__)
# print(Soldier.__name__)
# print(isinstance(soldier1, Soldier))
# print(hasattr(soldier1, "power"))
# print(getattr(soldier1, "power1", 5))
setattr(soldier1, "magic", 50)
# print(soldier1.magic)
# print(soldier1.power)