【发布时间】:2020-07-06 22:02:37
【问题描述】:
我是 Python 新手,我正在尝试将一个类中的变量用于另一个类,但幸运的是我不明白该怎么做。
我已经写了下面的代码。在 de World 类内部,我有一个 while 循环,在循环内部是 deltaTime。我想将 deltaTime 传递给我使用 deltaTime * 2 的类测试。
我该怎么做?
我找遍了,但我不明白解释。
import time as tm
class Test():
def __init__(self):
self.v = 10
v += 'here I need the deltaTime that is inside the while loop in the world class ''self.deltaTime' * 2
print(v)
这里是阶级世界
class World():
def __init__(self):
self.test = Test(self)
def run (self):
while True: # Main real-time simulation loop
# BEGIN mandatory statements
self.oldTime = self.time
self.time = tm.time () # The only place where the realtime clock is repeatedly queried
self.deltaTime = self.time - self.oldTime
# END mandatory statements
# ... other code, using objects that are in the world, like a racetrack and cars
print ('deltaTime ', self.deltaTime)
self.screen.update ()
tm.sleep (0.02) # Needed to free up processor for other tasks like I/O
world = World ()
world.run ()
【问题讨论】:
标签: python class variables methods