示例代码:
function Account:new (o)
o = o or {} -- create object if user does not provide one
setmetatable(o, self)
self.__index = self
return o
end
来源:
http://www.lua.org/pil/16.1.html
以下是需要翻译的内容:
什么是:
self.__index = self
line是什么?它为什么会在每次创建对象时执行?
new
函数内看到self.__index = self
语句,这让我认为它会在每个类的新实例中被执行。如果它只在基本表(在此情况下为Account)上设置一次,它不会产生完全相同的效果吗? - albizgil