zoukankan      html  css  js  c++  java
  • lua元方法

    lua中有元表的概念,元表类似于基类的功能,

    在元表中有两个方法可以很好的认识元表:

    __index和__newindex

    __index用于查询

    对表中的字段进行访问时,如果该表有元表,并且

    表中没有这个字段,就访问元表中的__index方法

    __newindex主要用于更新

    对表中的字段赋值时,会调用元表的__newindew方法

    如果__newindex是个函数,就会把表,key,value当成

    参数传进去

    如果__newindex是表,就会更新表中的字段信息

    例如

    local mt = {
    
            function sayhello()
    
                  print("hello")
    
            end
    
            __index = function()
    
                   print("the function is not exist")
    
             end
    
             __newindex = function(t, k, v)
    
                   print("t=",t, "k=",k, "v=", v)
    
             end
    
    }
    
     
    
    local m
    
    setmetable(m, mt)
    
    m.sayhello()
    
    m.num =1
  • 相关阅读:
    Hash大法
    最小表示法
    KMP算法题集
    分块总结
    2018 雅礼国庆集训
    二分图总结
    贪心总结
    Tire树总结(模板+例题)
    工具类文章合集
    网文胡乱汇总
  • 原文地址:https://www.cnblogs.com/xshang/p/7678611.html
Copyright © 2011-2022 走看看