問題描述
我們如何在函數輸入參數中輸入類型值作為對象? (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 user5370661、hjpotter92)