__index 与 __newindex Metamethod

--  13.4.1 The __index Metamethod   有点c++继承的意思
--
 create a namespace
Window  =  {}
--  create the prototype with default values
Window.prototype  =  {x = 0 , y = 0 , width = 100 , height = 101 , }  -- 定义一个原型表 默认值
--
 create a metatable
Window.mt  =  {}   -- 创建 metatable
--
 declare the constructor function
function  Window.new (o)  -- 定义new函数
     setmetatable (o, Window.mt)
    
return  o
end

-- 现在我们定义__index metamethod:
Window.mt.__index  =   function  (table, key)
    
return  Window.prototype[key]
end
-- 这样一来,我们创建一个新的窗口,然后访问他缺少的域结果如下:
=  Window.new{x = 10 , y = 20 }
print (w.width)        -- > 100
--
无需把__index metamethod 定义为函数  可以直接给他一个表
Window.mt.__index  =  Window.prototype

-- 13.4.2 The __newindex Metamethod
--
__newindex metamethod用来对表更新,__index则用来对表访问
print ( " --对 w.witdh赋值试试 " ,w.width);
w.width
= 90 ;
print (w.width,Window.prototype.width);   --
print ( " --遍历下表看看 " )
for  i,j  in   pairs (w)   do
print (i,j)
end               -- 很明显 对 width的赋值 直接导致 吧值赋值给了 w表本身
--
__newindex metamethod  现在试试__newindex
Window.mt.__newindex  =  Window.prototype
print ( " --定义了 __newindex  " )
=  Window.new{x = 10 , y = 20 }   -- 重新创建表 因为之前的表已经被污染了
print ( " --对 w.witdh赋值试试 " ,w.width);
w.width
= 90 ;
print (w.width,Window.prototype.width);
print ( " --遍历下表看看 " )
for  i,j  in   pairs (w)   do
print (i,j)
end

转载于:https://www.cnblogs.com/orez88/articles/2139073.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值