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)

    });

  • 相关阅读:
    第二章整理
    汇编实验二
    汇编实验一
    第一章整理
    第一部分 | 第1章 —— Hello Cocos2d-x
    返回 *this 的成员函数以及 const重载
    C++中的const
    680. Valid Palindrome II
    字典树
    单调队列
  • 原文地址:https://www.cnblogs.com/justart/p/11650347.html
Copyright © 2011-2022 走看看