root/lang/lua/misc/Class.lua @ 9650

Revision 234, 0.6 kB (checked in by cho45, 6 years ago)

lua,
lua/misc,
lua/misc/INI.lua,
lua/misc/t-sandbox.lua,
lua/misc/Test.lua,
lua/misc/t-ini.lua,
lua/misc/Class.lua,
lua/misc/t-class.lua,
lua/misc/List.lua,
lua/misc/t-list.lua,
lua/misc/SandBox.lua:

Lua の簡単なライブラリ

Line 
1
2function Class (obj)
3        -- metatable も __index も同じにする (再帰参照)
4        -- 演算子オーバーロードも直感的に書けるように
5        obj.__index = obj
6        if obj.super then
7                -- superclass の設定
8                setmetatable(obj, { __index = obj.super })
9                -- 比較演算子はコピーする
10                local events = {"eq", "lt", "le"}
11                for i, v in ipairs(events) do
12                        local name = "__"..v
13                        if not rawget(obj, name) then rawset(obj, name, obj.super[name]) end
14                end
15        end
16        return setmetatable({
17                new = function(...)
18                        local newObj = {}
19                        setmetatable(newObj, obj)
20                        newObj:initialize(unpack(arg))
21                        return newObj
22                end
23        }, obj)
24end
25
26
27return true
28
Note: See TracBrowser for help on using the browser.