<span style=
"font-family: 黑体;"
>--[[
Set = {}
mt = {} --元表
?
function Set.
new
(l)
????
local
set
= {}
????
setmetatable(
set
,mt)
????
for
_,v
in
ipairs(l)
do
????????
set
[v] =
true
????
end
????
return
set
end
?
?
--================================================
function Set.tostring(
set
)
????
local l = {}
????
for
e
in
pairs(
set
)
do
????????
l[#l + 1] = e
????
end
????
return
"{"
.. table.concat(l,
","
) ..
"}"
end
?
?
function Set.print(s)
????
print(Set.tostring(s))
end
?
?
--1 加(__add),并集===============================
function Set.union(a,b)
--[[???
if
getmetatable(a) ~= mt or getmetatable(b) ~= mt then
????????
error(
"attemp to 'add' a set with a non-set value"
,2)?? --error第二个参数的含义P116
????
end]]
????
local res = Set.
new
{}
????
for
k
in
pairs(a)
do
res[k] =
true
end
????
for
k
in
pairs(b)
do
res[k] =
true
end
????
return
res
end
?
s1 = Set.
new
{10,20,30,50}
s2 = Set.
new
{30,1}
--print(getmetatable(s1))
--print(getmetatable(s2))
?
mt.__add = Set.union
?
s3 = s1 + s2
--Set.print(s3)
?
--[[元表混用
s = Set.
new
{1,2,3}
s = s + 8
Set.print(s + 8)
]]
?
--2 乘(__mul),交集==============================
function Set.intersection(a,b)
????
local res = Set.
new
{}
????
for
k
in
pairs(a)
do
????????
res[k] = b[k]
????
end
????
return
res
end
?
mt.__mul = Set.intersection
?
--Set.print(s2 * s1)
?
?
?
--3 关系类===================================NaN的概念====
mt.__le = function(a,b)
????
for
k
in
pairs(a)
do
????????
if
not b[k] then
return
false
end
????
end
return
true
end
?
mt.__lt = function(a,b)
????
return
a <= b and not (b <= a)
end
?
mt.__eq = function(a,b)?????????? --竟然能这么用!?----
????
return
a <= b and b <= a
end
?
g1 = Set.
new
{2,4,3}
g2 = Set.
new
{4,10,2}
print(g1 <= g2)
print(g1 < g2)
print(g1 >= g2)
print(g1 > g2)
print(g1 == g1 * g2)
?
--]]
?
--============================================
--4 table访问的元方法=========================
--[[
--__index有关继承的典型示例
Window = {}
Window.prototype = {x = 0,y = 0,width = 100,height}
Window.mt = {}
?
function Window.
new
(o)
????
setmetatable(o,Window.mt)
????
return
o
end
?
Window.mt.__index = function (table,key)
????
return
Window.prototype[key]
end
?
w = Window.
new
{x = 10,y = 20}
print(w.width)
?
--__index修改table默认值
function setDefault (t,d)
????
local mt = {__index = function ()
return
d end}
????
setmetatable(t,mt)
end
?
tab = {x = 10,y = 20}
print(tab.x,tab.z)
setDefault(tab,0)
print(tab.x,tab.z)
?
--]]
?
--13.4.5 只读的table
function readOnly(t)
????
local proxy = {}
????
local mt = {
????
__index = t,
????
__newindex = function(t,v)
????????
error(
"attempt to update a read-only table"
,2)
????
end
????
}
????
setmetatable(proxy,mt)
????
return
proxy
end
?
days = readOnly{
"Sunday"
,
"Monday"
,
"Tuesday"
,
"W"
,
"T"
,
"F"
,
"S"
}
print(days[1])
days[2] =
"Noday"
</span>