字段

类

-- A default shared by all objects: in the class
local Game = {developer = "XYZ"}
Game.__index = Game

function Game.new()
  -- Fields are entries of the table
  return setmetatable({name = "", year = 0}, Game)
end

function Game:show_developer()
  print(self.developer)
end

local game = Game.new()
-- prints XYZ
game:show_developer()

game.year = 1999
-- A typo is not an error: it makes a new field
game.yaer = 2000
local year = game.year
-- year = 1999
local typo = game.yaer
-- typo = 2000

print("year = " .. year)
print("typo = " .. typo)