GVKun编程网logo

lua __index __newindex upvalue 示例(lua __index和__newindex)

22

最近很多小伙伴都在问lua__index__newindexupvalue示例和lua__index和__newindex这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展Lua--

最近很多小伙伴都在问lua __index __newindex upvalue 示例lua __index和__newindex这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展Lua -- __index元方法、lua --- __newindex 的使用规则、lua setmetatable __index __newindex、lua 之__index/__newindex的理解等相关知识,下面开始了哦!

本文目录一览:

lua __index __newindex upvalue 示例(lua __index和__newindex)

lua __index __newindex upvalue 示例(lua __index和__newindex)

项目中有个公会对象,数据大部分存在data中,之前都是 u.data.point这样访问,太麻烦了。

于是通过设置__index 使之可以直接访问属性,u.point。

但是还是不能直接改属性,u.point = 4,所以再设置了__newindex。

 

在设置了setMetatable之后,不能直接给u添加新属性,因为设置了__newindex,新的属性将直接加到u.data中的。

 

 

 

 

通过修改__index和__newindex会获得不同的结果。

 

1.正确结果

3    103    3    3
4    103    4    4
4    103    103    103
4    104    104    104

 

2.错误的__newindex

3    103    3    3
4    103    4    4
4    103    103    103
104    103    103    103

3.错误的__index

3    103    3    3
4    103    4    4
4    103    4    103
4    104    4    104

 

 

 

这里影响程序的不同结果是upvalue导致的。

由于一般程序中有可能动态改data。所以建议用function设置__index和__newindex,尤其注意各个不同函数中self指向的是什么对象。

Lua -- __index元方法

Lua -- __index元方法

总结

以上是小编为你收集整理的Lua -- __index元方法全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

lua --- __newindex 的使用规则

lua --- __newindex 的使用规则

1、如果 __newindex 是一个函数,在给 table 不存在的字段赋值的时候,会调用这个函数
2.如果 __newindex 是一个table,在给 table 不存在的字段赋值的时候,会直接给 __newindex 的 table 赋值

测试用例:

 1 local Song = {name = "南山南"};
 2 
 3 local mt = 
 4 {
 5     __index = Song, 6     __newindex = function(table,key,val)
 7         print(key .. "是一个不存在的字段,不能对其赋值!");
 8     end
 9 };
10 
11 local animal = {}
12 setMetatable(animal,mt);
13 print(animal.name);
14 animal.name = "cat";  
15 
16 local tmp = {};
17 
18 local mt1 = 
19 {
20     __index = Song;
21     __newindex = tmp;
22 };
23 local animal1 = {};
24 setMetatable(animal1,mt1);
25 print(tmp.cat);
26 animal1.cat = "Cat";
27 print(tmp.cat);

编译结果:

分享图片

lua setmetatable __index __newindex

lua setmetatable __index __newindex

总结

以上是小编为你收集整理的lua setmetatable __index __newindex全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

lua 之__index/__newindex的理解

lua 之__index/__newindex的理解

--example:
local temp_table ={
	10,1,Index1 = "hello",Index2 = "world",Index3 = "lua",Index4 = "language",lang = "lua language",}

temp_table.__add = function(a,b) return 3 end


for _,Value in pairs(temp_table) do
	print(Value)
end

local temp_Metable_table = {
	Index1 = "temp_new_Metable_Index1",Index2 = "temp_new_Metable_Index2",Key = "temp_new_Metable_Key_end",}


for _,Metable_Value in pairs(temp_Metable_table) do
	print(Metable_Value)
end
--只能访问temp_table的方法
--setMetatable(temp_Metable_table,temp_table) --如果setMetable 为这种方式的话,那么我不能够对Metable进行原子操作
--print(temp_Metable_table + temp_table)

--如果是这种方式的话,我们只能访问它的原子,也就是它的数据成员
--[[setMetatable(temp_Metable_table,{__index = temp_table} ) 

print(temp_Metable_table[1])
print(temp_Metable_table["lang"])--]]

--如果是__newindex的话,我们可以访问原table,找到相关的key,除此之外,你还可以自己给原table添加数据成员
setMetatable(temp_Metable_table,{__newindex = temp_table} ) 
temp_Metable_table[5] = 100
print(temp_Metable_table[5])
print(temp_table[5])



Window = {}

Window.prototype = {x = 0,y = 0,width = 100,height = 100,}
Window.mt = {}
function Window.new(o)
	setMetatable(o,Window.mt)
	print(getMetatable(o))
	print(getMetatable(Window.mt))
	return o
end

Window.mt.__index = function (t,key) //由于__index 给赋予了funcion,一个匿名函数
	-- body
	return 1000
end

w = Window.new({x = 10,y = 20})
print(w.a) //在这里虽然没有a这个成员,但是会默认a为__index所绑定的function返回值作为a的值

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.wangbin = "55"
print(w.wangbin)

总结:

如果在元table中去找相应的操作,例如__index,__newindex等,如果有则直接访问,如果没有就新添加进元table中

关于lua __index __newindex upvalue 示例lua __index和__newindex的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Lua -- __index元方法、lua --- __newindex 的使用规则、lua setmetatable __index __newindex、lua 之__index/__newindex的理解等相关知识的信息别忘了在本站进行查找喔。

本文标签: