[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: inheritance example
- From: RLake@...
- Date: Wed, 16 Jul 2003 17:22:36 -0500
> May I have an inheritance example please?
base = {}
function base._new(class, words)
return setmetatable(words, {__index = class})
end
function base:new(...)
return base._new(self, arg)
end
function base:punctuate()
return table.concat(self, self.sep)
end
function base:stream()
return table.concat(self, " ")
end
base.sep = ", "
----
dotty = base:new()
dotty.sep = ". "
---
-------- Sample:
U:\>lua
Lua 5.0 Copyright (C) 1994-2003 Tecgraf, PUC-Rio
> obj1 = base:new("Hello", "World")
> obj2 = dotty:new("Hello", "World")
> =obj1:punctuate()
Hello, World
> =obj2:punctuate()
Hello. World
> =obj1:stream()
Hello World
> =obj2:stream()
Hello World
>
------------
Some may object to the style; but it has served me reasonably well.
Note that a lot of identical metatables are created; memoising their
creation would probably be a good idea.
Rici.