lua _index,newindex,rawget和rawset

1 零、元表的概念

对Lua中元表的解释: 元表能够改变表的行为模式。ide

这里举个例子:

Window = {}

Window.prototype = {x = 0 ,y = 0 ,width = 100 ,height = 100,}

Window.mt = {}

function Window.new(o)
	setmetatable(o ,Window.mt)
	return o
end

Window.mt.__index = Window.prototype

Window.mt.__newindex = function (table ,key ,value)
	if key == "wangbin" then
		rawset(table ,"wangbin" ,"yes,i am")
	end
end

w = Window.new{x = 10 ,y = 20}
w.wangbin = "55"
print(w.wangbin)

而后,咱们能够看到打印信息是:yes,i amlua

本来赋值的地方是w.wangbin = "55",可是结果倒是 yes,i am。spa

这里就改变了元表的行为模式。

2、__index的理解

__index是:当咱们访问一个表中的元素不存在时,则会触发去寻找__index元方法,若是不存在,则返回nil,若是存在,则返回结果

Window = {}

Window.prototype = {x = 0 ,y = 0 ,width = 100 ,height = 100,}
Window.mt = {}
function Window.new(o)
	setmetatable(o ,Window.mt)
	return o
end
Window.mt.__index = function (t ,key)
	-- body
	return 1000
end
w = Window.new{x = 10 ,y = 20}
print(w.wangbin)

打印结果是:1000。这里能够看出,咱们在new的时候,w这个表里其实没有wangbin这个元素的,咱们重写了元表中的__index,使其返回1000,意思是:若是你要寻找的元素,该表中没有,那么默认返回1000。

Window = {}
 
Window.prototype = {x = 0 ,y = 0 ,width = 100 ,height = 100,}
Window.mt = {}
function Window.new(o)
	setmetatable(o ,Window.mt)
	return o
end
Window.mt.__index = function (t ,key)
	-- body
	return 1000
end
w = Window.new{x = 10 ,y = 20}
w.f = 10
print(w.f)

输出结果:10

2、__newindex的理解

__newindex:当给你的表中不存在的值进行赋值时,lua解释器则会寻找__newindex元方法,发现存在该方法,则执行该方法进行赋值,注意,是使用rawset来进行赋值,至于缘由,后面会讲到。

Window.mt = {}
function Window.new(o)
	setmetatable(o ,Window.mt)
	return o
end
Window.mt.__index = function (t ,key)
	return 1000
end
Window.mt.__newindex = function (table ,key ,value)
	if key == "wangbin" then
		rawset(table ,"wangbin" ,"yes,i am")
	end
end
w = Window.new{x = 10 ,y = 20}
w.wangbin = "55"
print(w.wangbin)

ok,这里的打印结果是:yes,i am。w这个表里原本没有wangbin这个元素的,咱们重写了元表中__newindex,并在__newindex方法中从新进行赋值操做,而后,咱们对这个本不存在的原色w.wangbin进行赋值时,执行__newindex方法的赋值操做,最后,打印结果即是:yes,i am

Window ={}
Window.mt = {}
function Window.new(o)
	setmetatable(o ,Window.mt)
	return o
end
Window.mt.__index = function (t ,key)
	return 1000
end
Window.mt.__newindex = function (table ,key ,value)
	if key == "wangbin" then
		rawset(table ,"wangbin" ,"yes,i am")
	end
end
w = Window.new{x = 10 ,y = 20}
w.x = "55"
print(w.x)

输出结果:55

4、rawget和rawset的理解

awget是为了绕过__index而出现的,直接点,就是让__index方法的重写无效。(我这里用到"重写"二字,可能不太对,但愿能获得纠正)

Window = {}

Window.prototype = {x = 0 ,y = 0 ,width = 100 ,height = 100,}
Window.mt = {}
function Window.new(o)
	setmetatable(o ,Window.mt)
	return o
end
Window.mt.__index = function (t ,key)
	return 1000
end
Window.mt.__newindex = function (table ,key ,value)
	if key == "wangbin" then
		rawset(table ,"wangbin" ,"yes,i am")
	end
end
w = Window.new{x = 10 ,y = 20}
print(rawget(w ,w.wangbin))

打印结果是:nil。这里的元表中__index函数就再也不起做用了。

可是rawset呢,起什么做用呢?咱们再来运行一段代码。

Window = {}
Window.prototype = {x = 0 ,y = 0 ,width = 100 ,height = 100,}
Window.mt = {}
function Window.new(o)
	setmetatable(o ,Window.mt)
	return o
end
Window.mt.__index = function (t ,key)
	return 1000
end
Window.mt.__newindex = function (table ,key ,value)
	table.key = "yes,i am"
end
w = Window.new{x = 10 ,y = 20}
w.wangbin = "55"

而后咱们的程序就stack overflow了。可见,程序陷入了死循环。由于w.wangbin这个元素原本就不存在表中,而后这里不断执行进入__newindex,陷入了死循环

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值