zoukankan      html  css  js  c++  java
  • 牛横的元表

    1、childtable=setmetatable({},{

             A :__index=function(childtable,key)

                             end

             B:__index={xkey=6}

             以上两种当访问子表不存在的键时,会到元表中查找__index,如果不存在返回nil

             C:__newindex=function(childtable,k,v)

                                        rawset(childtable,k,v);--可以利用这个特性,当给表赋值时,进行校验之类操作

                                  end

             D:__newindex={}

             以上两种情况,当给子表赋予不存在的键值时,会去原表中找__newindex

             E:运算符

                __add(childtable,newtable)--当childtable+newtable时调用

             F:__call(childtable,newtable)--当childtable(newtable)时,调用

            G:__tostring(mytable)--当print(childtable)

              mytable = setmetatable({ 10, 20, 30 }, {
                             __tostring = function(mytable)
                            sum = 0
                           for k, v in pairs(mytable) do
                              sum = sum + v
                            end
                  return "表所有元素的和为 " .. sum
              end
    })
    print(mytable)

    });

  • 相关阅读:
    laravel吐槽系列之一
    每日晨读_20140924
    技术晨读_2014_9_1
    大话胖model和瘦model
    大话PHP缓存头
    vim黏贴自动增加tab的毛病
    Laravel学习
    郑捷2017年电子工业出版社出版的图书《NLP汉语自然语言处理原理与实践》
    delete
    NLP知识结构概述
  • 原文地址:https://www.cnblogs.com/justart/p/11650347.html
Copyright © 2011-2022 走看看