簡體   English   中英

如何在 python 中使用一個變量從一個 class 到另一個變量?

[英]how do I use a variable from one class into another in python?

我是 Python 的新手,我正在嘗試使用從一個 class 到另一個 class 的變量,但幸運的是我不明白該怎么做。

我已經寫了下面的代碼。 在de World class里面我有一個while循環,循環里面是deltaTime。 我想將 deltaTime 傳遞給我使用 deltaTime * 2 的 class 測試。

我怎樣才能做到這一點?

我已經四處尋找,但我不明白解釋。

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 世界

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 ()

當您執行self.test = Test(self)時,您將self傳遞給Test 但是您沒有將其作為參數添加到Test.__init__方法中。 它應該是:

class Test():
    def __init__(self, world):
        self.v = 10
        self.world = world

        self.v += world.deltaTime * 2
        print(v)

但是當你調用Test(self)時,你還沒有填寫self.deltaTime 您應該通過從while循環調用的Test class 中的不同方法使用它。

class Test():

    def __init__(self, world):
        self.v = 10
        self.world = world

    def display_time(self):
        self.v += world.deltaTime * 2
        print(self.v)

然后從World.run()調用test.display_time() ()

您需要在代碼中創建Test object:

t_object = Test(...)

並將 object 插入到運行 function 中:

def run(self, t_object):

然后每次你想增加 v 值時,你可以增加屬性:

t_object.v += self.deltaTime
print(t_object.v)

所以這里的行是不需要的:

v += 'here I ne ... 
print(v)

**事實上,像你對這兩行所做的那樣做是錯誤的。因為它不是更新屬性,而是一個新的變量名 v。

** 如果沒有 inheritance,也不需要在 class 名稱之后加上 ()。 在這種情況下,只有這種方式是可以的:

class Name:

我認為您在談論class 組合

import time as tm

class Test():
  def __init__(self):
    self.v = 10

  def use_world_deltatime(self, delta_time):
    v += self.deltaTime * 2
    print(v)

class World():
    def __init__(self):
        self.test = Test()

    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
            self.test.use_world_deltatime(self.deltaTime)
            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()

基本上,您將測試 class 創建為獨立的 object 並在 World class 中實例化它。 完成此操作后,您可以隨時使用 Test 的方法(如use_world_deltatime )。

是否有意義?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM