首页 > 解决方案 > Lua 从父表修改元表属性

问题描述

我有这段代码,但我不希望tbl变量得到widthheight分配给它,而是我希望base_table'swidth和properties 被修改为 200,而height不是tbl被分配200。heightwidth

function deepCopy(original)
    local copy = {}
    for k, v in pairs(original) do
        if type(v) == "table" then
            v = deepCopy(v)
        end
        copy[k] = v
    end
    return copy
end

function PrintTable(t)
    for k, v in pairs(t) do
        if v == "table" then
            PrintTable(v)
        else
            print(k, v)
        end
    end
end

local base_table = {
  width = 0,
  height = 0,
  x = 0,
  y = 0,

  SetSize = function(self, w, h)
    self.width, self.height = w, h
  end,

  SetPos = function(self, x, y)
    self.x, self.y = x,y
  end

}

local tbl = {}

local meta = {__index = deepCopy(base_table)}

setmetatable(tbl, meta)


PrintTable(tbl) -- prints nothing

tbl:SetSize(200, 200)

PrintTable(tbl) -- prints height 200, width 200 which I don't want, I want it to print nothing like above. though if I do tbl.x I want it to 200, since it would be on the metatable, rather than the tbl itself 

标签: luametatable

解决方案


local function deepCopy(original)
   local copy = {}
   for k, v in pairs(original) do
      if type(v) == "table" then
         v = deepCopy(v)
      end
      copy[k] = v
   end
   return copy
end

local function PrintTable(t)
   for k, v in pairs(t) do
      if v == "table" then
         PrintTable(v)
      else
         print(k, v)
      end
   end
end

local base_table = {
   width = 0,
   height = 0,
   x = 0,
   y = 0,

   SetSize = function(self, w, h)
      self.width, self.height = w, h
   end,

   SetPos = function(self, x, y)
      self.x, self.y = x,y
   end

}

local tbl = {}
local cpy = deepCopy(base_table)
setmetatable(cpy, {__newindex = function(_, k, v) rawset(tbl, k, v) end})
setmetatable(tbl, {__index = cpy; __newindex = cpy})

PrintTable(tbl) -- prints nothing

tbl:SetSize(200, 200)
tbl.someproperty = "some value"

PrintTable(tbl) -- prints only someproperty

推荐阅读