List = {}
List.new = function()
return {first= 1 ,last= 1}
end
List.pushback = function(list,value)
local last = list.last + 1;
list.last = last;
List[last] = value;
end
List.pop = function(list)
local first = list.frist;
if first > list.last then
error("list is empty")
end
local value = list[first];
list[first] = nil;
return value;
end
local buff = {};
for line in io.lines() do
t[#t + 1] = line;
end
local all = table.concat(t,'\n');
io.read("*all") -- 采取了上面算法来创建大的字符串。
--data文件内容--
Entry({"Jim","Green","Male","14","American"})
--代码文件
count = 0
Entry = function(item)
for k,v in pairs(item) do
print(v);
count = count+1;
end
end
dofile('data');
function gettable_event (table, key)
local h
if type(table) == "table" then
local v = rawget(table, key)
-- if key is present, return raw value
if v ~= nil then
return v
end
h = metatable(table).__index
if h == nil then
return nil
end
else
h = metatable(table).__index
if h == nil then
error(···)
end
end
if type(h) == "function" then
return (h(table, key)) -- call the handler
else
return h[key] -- or repeat operation on it
end
end
Window = {}
Window.prototype = {x=0,y=0,width=100,height=100}
Window.mt = {__index=function(tbl,key) return Window.prototype[key] end}
Window.new = function(obj)
setmetatable(obj,Windows.mt);
return obj;
end
win = Window.new({x=30,y=30});
print(win.width) -- 100
Window.mt = {__index=Window.prototype}
当需要使用同一个库的不同版本,可以利用require的一个特性:如果模块名中包含连字符,如1.4-zlib.so,那么其只使用连字符后的内容来创建调用函数,本例为luaopen_zlib
function require(name)
if not package.loaded[name] then
local loader = findloader(name)
if loader == nil then
error("unable to load module"..name)
end
package.loaded[name] = true
local res = loader(name)
if res ~= nil then
package.loaded[name] = res
end
end
return package.loaded[name]
end
对require的实现逻辑分析后,需要注意两点:1 如果准备用lua脚本实现模块,则不要与静态预加载的C模块名重名,否则只会加载系统的内置C模块。2 如果加载的lua模块的文件名与文件内使用module声明的模块名不相同时,将导致返回一个NIL值。只有二者相同时,才返回一个同名的table。
local M = {}
_G['modname'] = M
package.loaded['modname'] = M
--module code ...
local M = {}
_G['modname'] = M
package.loaded['modname'] = M
setmetatable(M,{__index=_G}) --local _G = _G
setfenv(1,M)
--module code ...
module('name'[,package.seeall]) --后续代码即可像写普通的lua代码一样
Account = { balance = 0;
withdraw = function(self,v) self.balance -= v; end
deposite = function(self,v) self.balance += v; end
}
Account:query = function(v) return self.balance;end
a = Account;
a.deposite(a,500);
a.withdraw(a,100);
a:query();
Account:new = function(obj)
if not obj then
obj = {}
end
self.__index = self
setmetatable(obj,self)
return obj;
end
a = Account:new()
a.deposite(100)
SpecialAccount = Account:new()
sa = SpecialAccount:new({limit=1000})
对象sa及相关类table的关系如下图:
local function search (key,list) --search the first matched key
for i=1,#list do
local val = plist[i][key]
if val then
return val
end
end
end
function createClass(...)
local c = {}; -- new class
local parents = {...} -- parent class from args
local finder = function(tbl,key)
return search(key,parents)
end
setmetatable(c,{__index = finder });
c.__index = c;
c:new = functon(obj)
if not obj
obj = {}
end
setmetatable(obj,c)
return obj
end
return c;
end
Named = {}
Named.getname = function(self)
return self.name
end
Name.setName = function(self,n)
self.name = n
end
--现在以Named和Account为基类,创建新类
NamedAccount = createClass(Account,Named);
na = NamedAccount:new({name="Jobs"})
print(na.getname())
newAccount = function(initBalance)
local self = {balance=initBalance} --private data
local withdraw = function(v) --public function
self.balance -= v;
end
local deposit = function(v) --public function
self.balance -= v;
end
local query = function() --public function
return self.balance;
end
local method = function() --private function
...
end
return {
withdraw = withdraw,
deposit = deposit,
query = query
}
end
--使用时
account = newAccount(100)
account.withdraw(40)
print(account.query())
如果使用弱引用表,则可以自动回收不用的条目,避免表变的过大而影响计算过程。当然也会出现中间发生垃圾回收,导致需要重新计算的问题。