lua-users home
lua-l archive

Re: creating a new module as an extention of another module

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mar 31, 2006, at 17:27, david herviou wrote:
I think it's now very clean with this new package module system but I have a question on how to do a module
that is an extension of an another module?
Here is another variation on module and inheritance:
function extends( aSuper )
 if type( aSuper ) == "string" then
 aSuper = require( aSuper )
 end
 aSuper = aSuper or { __index = _G }
 return function( aClass )
 setmetatable( aClass, aSuper )
 aClass.__index = aClass
 aClass.self = aClass
 aClass.super = aSuper
 return aClass
 end
end
-- define the root class
module( "Object", extends() )
function self:doIt()
 return "do it"
end
-- define a subclass
module( "MyObject", extends( "Object" ) )
function self:doIt()
 return "about to " .. super.doIt( self )
end
-- define another subclass
module( "MyOtherObject", extends( "MyObject" ) )
function self:doIt()
 return super.doIt( self ) .. " well"
end
print( self:doIt() )
> about to do it well
Cheers
--
PA, Onnay Equitursay
http://alt.textdrive.com/

AltStyle によって変換されたページ (->オリジナル) /