Lua元方法的使用
来源:互联网 发布:怎么弄网络平台卖东西 编辑:程序博客网 时间:2024/04/28 18:41
Set = {}Set.mt = {}function Set.new( t )local set = {}setmetatable( set, Set.mt )for _, l in ipairs(t) do set[l] = true endreturn setendfunction Set.union( a, b )if getmetatable( a ) ~= Set.mt or getmetatable( b ) ~= Set.mt thenerror("attempt to add a set with a non-set value",2)endlocal res = Set.new{}for k in pairs(a) do res[k] = true endfor k in pairs(b) do res[k] = true endreturn resendfunction Set.intersection( a, b )local res = Set.new{}for k in pairs(a) dores[k] = b[k]endreturn resendSet.mt.__le = function( a, b )for k in pairs( a ) doif not b[k] then return false endendreturn trueendSet.mt.__lt = function( a, b )return a <= b and not ( b <= a )endSet.mt.__eq = function( a, b )return a <= b and b <= aendfunction Set.tostring( set )local s = "{"local sep = ""for e in pairs( set ) dos = s .. sep .. esep = ", "endreturn s .. "}"endfunction Set.print( s )print( Set.tostring(s))endSet.mt.__add = Set.unionSet.mt.__mul = Set.intersectionSet.__tostring = Set.tostring--算数s1 = Set.new({10,20,30,50})s2 = Set.new({30,1})s3 = s1 + s2s4 = s3 * s2Set.print( s3 )Set.print( s4 )s6 = Set.new({8})s5 = s4 + s6Set.print( s5 )--关系t1 = Set.new{2,4}t2 = Set.new{4,10,2}print( t1 <= t2 )print( t1 < t2 )print( t1 >= t1 )print( t1 > t1 )print( t1 == t1 * t2 )
0 0
- Lua元方法的使用
- Lua中__newindex元方法的使用
- Lua元表的使用
- lua 继承,使用元方法__index
- lua table访问的元方法
- lua元方法的简单介绍
- Lua元表中__index元方法的作用
- Lua 元表 元方法
- lua元方法__index
- lua元方法__newindex
- Lua -- __index元方法
- lua 元方法
- Lua元表的定义和使用
- lua的元表
- lua的元表
- lua的元表
- Lua 中的元表和元方法
- Lua中的元表和元方法
- 1197 Specialized Four-Digit Numbers
- 【CCAI大咖秀】AlphaGo/Mobileye教父:智能科学需要融合机器学习、计算神经科学与认知科学
- new一个对象的时候JVM都做了哪些事情
- 精述IBM的MQTT协议和MQTT-S协议
- 【JavaScript】类型转换
- Lua元方法的使用
- Android开发之连接MySQL数据库(一)
- 单链表的实现
- [5]白盒测试方法2—图覆盖准则
- Android SQLite数据库
- vim配置及插件安装管理(超级详细)
- Lua元表的使用
- HDOJ杭电1004 Let the Balloon Rise
- 黑客讲述渗透Hacking Team全过程(详细解说)