lua利用metatable重载操作符测试代码

m = {}
m.__add = function(...) print("add","+",...) end
m.__sub = function(...) print("sub","-",...) end
m.__mul = function(...) print("mul","*",...) end
m.__div = function(...) print("div","/",...) end
m.__mod = function(...) print("mod","%",...) end
m.__pow = function(...) print("pow","^",...) end
m.__unm = function(...) print("unm","-",...) end
m.__concat = function(...) print("concat","..",...) end
m.__len = function(...) print("len","#",...) end --only func on obj is not string or table
m.__eq = function(...) print("eq","==",...) end
m.__lt = function(...) print("lt","<",...) end
m.__le = function(...) print("le","<=",...) end
m.__index = function(...) print("index",...) end
m.__newindex = function(...) print("newindex",...) end
m.__call = function(...) print("call","()",...) end


o = {}
o2 = {}
setmetatable(o,m)
setmetatable(o2,m)
t=o+1
t=o-2
t=o*3
t=o/4
t=o%5
t=o^6
t=-o
t=o..8
t=#o --cause o is table,__len not be used
t=(o==o2) --must same type and can not be number or string
t=(o<o2)
t=(o<=o2)
t=o[9]
o[10] = 10
t=o(11)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值