File 如何重命名文件对象方法

File 如何重命名文件对象方法,file,methods,lua,rename,File,Methods,Lua,Rename,我试图复制file对象的一些方法,就像这样,因为我需要使用大写方法而不是小写方法来测试luaapi代码。所以我试了一下: function openFile(n, m) local f = io.open(n, m) if(not f) then return status(nil,"openFile: Nofile: "..tostring(n)) end f.Read = f.read f.Write = f.write f.Close = f.close

我试图复制file对象的一些方法,就像这样,因为我需要使用大写方法而不是小写方法来测试luaapi代码。所以我试了一下:

function openFile(n, m)
  local f = io.open(n, m)
  if(not f) then
    return status(nil,"openFile: Nofile: "..tostring(n))
  end
  f.Read  = f.read
  f.Write = f.write
  f.Close = f.close
  f.Flush = f.flush
  return f
end
考虑到文件对象
文件:读取
文件:写入
。。。阿卡

但给了我一个错误,如下所示:

Execution error:
.\ZeroBraineProjects/dvdlualib/fileapi.lua:41: attempt to index local 'f' (a userdata value)
我认为文件是简单的对象表。事实证明,它是一个userdata对象


那么,伙计们,我如何使用大写字母的方法进行读写呢?

您必须在
f
的元表中设置这些字段:

local m = getmetatable(f)
m.Read  = m.read
m.Write = m.write
m.Close = m.close
m.Flush = m.flush

我就是这么做的。在编辑注释之前。谢谢