我們如何在函數輸入參數中輸入類型值作為對象? (How do we input type value as object in function input parameter?)


問題描述

我們如何在函數輸入參數中輸入類型值作為對象? (How do we input type value as object in function input parameter?)

我想問一下如何比較一個對像或使用一個對像作為lua函數中的參數輸入,我來自java所以它類似於這樣

public void steal(Person other, double value) { 
    other.balance ‑= value;
}

我們看到有Person對像作為參數你如何在lua中做到這一點?因為似乎 lua 中的每個函數都沒有像

function steal(value)
    self.balance = self.balance ‑ value ‑‑ i know it's wrong since it reference to self but i think it something like this
end
這樣的類型值

值可以是任何值,因為 lua 不是原始 OOP 所以我不知道該怎麼做,但我一直在學習一些 OOP在interwebs上的lua中,卡在這一點上,我們如何輸入值作為對象?


參考解法

方法 1:

Just pass the variable?

function steal(whatever, value)
    whatever.balance = whatever.balance ‑ value
end

(by user5370661hjpotter92)

參考文件

  1. How do we input type value as object in function input parameter? (CC BY‑SA 2.5/3.0/4.0)

#lua #function #object #OOP






相關問題

使用 MSXML2.ServerXMLHTTP 從網頁訪問數據會在 Lua 中返回截斷的數據 (Using MSXML2.ServerXMLHTTP to access data from a web page returns truncated data in Lua)

如何在 VS 2008 中包含 Lua 庫 (How can I include Lua library in VS 2008)

Corona 中的 iPad 式慣性滾動 (iPad-style inertial scrolling in Corona)

Lua cư xử kỳ lạ trên nền tảng PowerPC / LynxOS, tại sao? (Lua behaves weird on PowerPC/LynxOS platform, why?)

我們如何在函數輸入參數中輸入類型值作為對象? (How do we input type value as object in function input parameter?)

反編譯 Lua 字節碼的最佳工具? (Best tool(s) for decompiling Lua bytecode?)

本機 Lua 中的高效可變字節數組 (Efficient mutable byte array in native Lua)

在 Lua 中字符串化對象名稱 (Stringify object name in Lua)

純 Lua 中的全功能正則表達式庫 (Fully-featured regex library in pure Lua)

自我作為參數,並設置範圍? (self as param, and setting scope?)

已經放 } 但錯誤仍然說 } 是預期的? (Already put } but the error still says that } is expected?)

我想使用 HPC 的 gpu 並嘗試 module add CUDA ...但出現錯誤。錯誤是“Lmod 檢測到以下錯誤: (I want to use the gpu of the HPC and try module add CUDA... But errors occurs. The error is "Lmod has detected the following error:)







留言討論