| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- # -*- coding: UTF-8 -*-
- # def Foo():
- # n1 = 20
- # n2 = 10
- # def add_item():
- # print(n1 + n2)
- # def reduce():
- # print(n1 - n2)
- # return {"add": add_item, "reduce": reduce}
- # obj = Foo()
- # obj["add"]()
- # obj["reduce"]()
- ###============ 类的写法 ============###
- # class Foo():
- # def __init__(self, n1, n2):
- # self.n1 = n1
- # self.n2 = n2
-
- # def add_item(self):
- # print(self.n1 + self.n2)
- # def reduce(self):
- # print(self.n1 - self.n2)
- # obj = Foo(20, 5)
- # obj.add_item()
- # obj.reduce()
- ###============ 传值 ============###
- # class Foo():
- # def __init__(self, n1):
- # self.n1 = n1
-
- # def add_item(self,n2):
- # print(self.n1 + n2)
- # def reduce(self, n2):
- # print(self.n1 - n2)
- # obj = Foo(20)
- # obj.add_item(5)
- # obj.reduce(1)
- ###============ 传值 ============###
- # class Foo():
- # def __init__(self, n1):
- # self.n1 = n1
- # def add_item(self, n2):
- # print(self.n1 - n2()) # 定义成什么类型,传的就是什么类型
- # # print(self.n1 - n2[])
- # def f():
- # return 30
-
- # obj = Foo(20)
- # obj.add_item(f)
- # # obj.add_item([1])
- ###============ 攻击别的英雄 ============###
- class AP():
- def __init__(self, h, atc):
- self.h = h
- self.atc = atc
-
- def attack(self,obj):
- """ 攻击另一个英雄 """
- # 攻击代码
- # 被攻击的英雄生命值需要减去自己对象的攻击力
- obj.h = obj.h - self.atc
- print(f"{obj.h} ".format(obj.h))
- obj1 = AP(100, 10)
- obj2 = AP(200, 20)
- obj1.attack(obj2)
- print(obj1.h)
- obj1.h = 101
- print(obj1.h)
|