Lua 中的继承
Inheritance in Lua
我正在尝试制作一个简单的 api 来简化创建 classes 和子 classes 的过程。
function newClass(index,...)
local new_class = {}
setmetatable(new_class,{__index = index})
local optionalArgs = {...}
if optionalArgs[1] ~= nil then
setmetatable(new_class,optionalArgs[1])
end
return new_class
end
--TESTINGCODE
exampleSuper = newClass({name="Super",id=1,getName = function() print("Super") end,})
exampleSuper.getName()
exampleSub = newClass({name="Sub",},exampleSuper)
print(exampleSub.id)
问题是,即使我创建了一个名为 exampleSuper 的新超级 class,它的字段也没有提供给 exampleSub class。如何更改我的代码以允许我的函数定义子 class?
可能是这样的:
function newClass(new_obj,old_obj)
old_obj = old_obj or {} --use passed-in object (if any)
new_obj = new_obj or {}
assert(type(old_obj) == 'table','Object/Class is not a table')
assert(type(new_obj) == 'table','Object/Class is not a table')
old_obj.__index = old_obj --store __index in parent object (optimization)
return setmetatable(new_obj,old_obj) --create 'new_obj' inheriting 'old_obj'
end
这个问题在 Object-Oriented Programming chapter of Programming In Lua, specifically in the Inheritance 部分得到了很好的回答。
在您的特定情况下,当 optionalArgs[1] ~= nil
检查为真时,您不会在元表中设置 __index
,因为您会覆盖之前的分配。
在 newClass
中,您对 setmetatable
的第二次调用会简单地覆盖第一个。以下较短的函数将为您提供预期的行为:
function newClass(new_class, super)
new_class = new_class or {}
-- This assumes single inheritance. Multiple inheritance would require a
-- function for __index.
if super then
setmetatable(new_class,{__index = super})
end
return new_class
end
我正在尝试制作一个简单的 api 来简化创建 classes 和子 classes 的过程。
function newClass(index,...)
local new_class = {}
setmetatable(new_class,{__index = index})
local optionalArgs = {...}
if optionalArgs[1] ~= nil then
setmetatable(new_class,optionalArgs[1])
end
return new_class
end
--TESTINGCODE
exampleSuper = newClass({name="Super",id=1,getName = function() print("Super") end,})
exampleSuper.getName()
exampleSub = newClass({name="Sub",},exampleSuper)
print(exampleSub.id)
问题是,即使我创建了一个名为 exampleSuper 的新超级 class,它的字段也没有提供给 exampleSub class。如何更改我的代码以允许我的函数定义子 class?
可能是这样的:
function newClass(new_obj,old_obj)
old_obj = old_obj or {} --use passed-in object (if any)
new_obj = new_obj or {}
assert(type(old_obj) == 'table','Object/Class is not a table')
assert(type(new_obj) == 'table','Object/Class is not a table')
old_obj.__index = old_obj --store __index in parent object (optimization)
return setmetatable(new_obj,old_obj) --create 'new_obj' inheriting 'old_obj'
end
这个问题在 Object-Oriented Programming chapter of Programming In Lua, specifically in the Inheritance 部分得到了很好的回答。
在您的特定情况下,当 optionalArgs[1] ~= nil
检查为真时,您不会在元表中设置 __index
,因为您会覆盖之前的分配。
在 newClass
中,您对 setmetatable
的第二次调用会简单地覆盖第一个。以下较短的函数将为您提供预期的行为:
function newClass(new_class, super)
new_class = new_class or {}
-- This assumes single inheritance. Multiple inheritance would require a
-- function for __index.
if super then
setmetatable(new_class,{__index = super})
end
return new_class
end