计算.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # -*- coding: UTF-8 -*-
  2. # def Foo():
  3. # n1 = 20
  4. # n2 = 10
  5. # def add_item():
  6. # print(n1 + n2)
  7. # def reduce():
  8. # print(n1 - n2)
  9. # return {"add": add_item, "reduce": reduce}
  10. # obj = Foo()
  11. # obj["add"]()
  12. # obj["reduce"]()
  13. ###============ 类的写法 ============###
  14. # class Foo():
  15. # def __init__(self, n1, n2):
  16. # self.n1 = n1
  17. # self.n2 = n2
  18. # def add_item(self):
  19. # print(self.n1 + self.n2)
  20. # def reduce(self):
  21. # print(self.n1 - self.n2)
  22. # obj = Foo(20, 5)
  23. # obj.add_item()
  24. # obj.reduce()
  25. ###============ 传值 ============###
  26. # class Foo():
  27. # def __init__(self, n1):
  28. # self.n1 = n1
  29. # def add_item(self,n2):
  30. # print(self.n1 + n2)
  31. # def reduce(self, n2):
  32. # print(self.n1 - n2)
  33. # obj = Foo(20)
  34. # obj.add_item(5)
  35. # obj.reduce(1)
  36. ###============ 传值 ============###
  37. # class Foo():
  38. # def __init__(self, n1):
  39. # self.n1 = n1
  40. # def add_item(self, n2):
  41. # print(self.n1 - n2()) # 定义成什么类型,传的就是什么类型
  42. # # print(self.n1 - n2[])
  43. # def f():
  44. # return 30
  45. # obj = Foo(20)
  46. # obj.add_item(f)
  47. # # obj.add_item([1])
  48. ###============ 攻击别的英雄 ============###
  49. class AP():
  50. def __init__(self, h, atc):
  51. self.h = h
  52. self.atc = atc
  53. def attack(self,obj):
  54. """ 攻击另一个英雄 """
  55. # 攻击代码
  56. # 被攻击的英雄生命值需要减去自己对象的攻击力
  57. obj.h = obj.h - self.atc
  58. print(f"{obj.h} ".format(obj.h))
  59. obj1 = AP(100, 10)
  60. obj2 = AP(200, 20)
  61. obj1.attack(obj2)
  62. print(obj1.h)
  63. obj1.h = 101
  64. print(obj1.h)