簡體   English   中英

嘗試索引全局“BankAccount”(一個函數值)

[英]attempt to index global 'BankAccount' (a function value)

我是 Lua 的新手,所以在 lua 代碼中創建和訪問類時。 我收到以下錯誤

嘗試索引全局“BankAccount”(一個函數值)

代碼塊如下供參考。

-- lua 類

-- bank account is a table

BankAccount = {
  account_number = 0,
  holder_name = "",
  balance = 0.0
}

function BankAccount:deposit(amount)
  self.balance = self.balance + amount
end

function BankAccount(amount)
  self.balance = self.balance - amount
end

function BankAccount:new(t)
  t = t or {}
  setmetatable(t,self)
  self.__index= self
  return t
end
-- instantiate an object of the class BankAccount

johns_account = BankAccount:new({
  account_number = 12345678,
  holder_name = "John",
  balance = 0.0
})

print(johns_account.account_number)

誰能解釋我犯了什么錯誤或我遺漏了什么?

function BankAccount(amount)BankAccount重新定義為一個函數。

該行應該是function BankAccount:withdraw(amount)

暫無
暫無

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

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