• 了解lua setmetatable和__index的小例子


      local _a1 = {20, 1, key1 = "hello", key2 = "world", lang = "lua"}
      print("the table _a1:")
      for _,v in pairs(_a1) do
          print(v)
      end
    
      local _a2 = {
          key1 = "hello new",
          key2 = "world new"
      }
    
      print("\nthe old table _a2:")
      for _,v in pairs(_a2) do
          print(v)
      end
    
      print("\na2的metatable:",getmetatable(_a2))
      print("language:",_a2["lang"])
    
      -- 关注函数及__index
      setmetatable(_a2, {__index = _a1})
    
      print("\nthe new table _a2:")
      for _,v in pairs(_a2) do
          print(v)
      end
    
      print("\nlanguage:", _a2["lang"])
    

    the table _a1:
    20
    1
    hello
    lua
    world

    the old table _a2:
    hello new
    world new

    a2的metatable: nil
    language: nil

    the new table _a2:
    hello new
    world new

    language: lua

    --数组是从1开始的哈哈
      print("_a2数组第一个元素:", _a2[1])
      print("\n_a2的metatable:",getmetatable(_a2))

    小贴士:

    大家可以参考lua手册,metatable是被译作元表,Lua 中的每个值都可以用一个 metatable。这个 metatable 就是一个原始的 Lua table ,它用来定义原始值在特定操作下的行为。

    一个 metatable 可以控制一个对象做数学运算操作、比较操作、连接操作、取长度操作、取下标操作时的行为,metatable 中还可以定义一个函数,让 userdata 作垃圾收集时调用它。对于这些操作,Lua 都将其关联上一个被称作事件的指定健。当 Lua 需要对一个值发起这些操作中的一个时,它会去检查值中 metatable 中是否有对应事件。如果有的话,键名对应的值(元方法)将控制 Lua 怎样做这个操作。

    metatable通过其包含的函数来给所挂接的table定义一些特殊的操作,包括:

    __add: 定义所挂接table的加法操作
    __mul: 定义乘法操作
    __div: 定义除法操作
    __sub: 定义减法操作
    __unm: 定义负操作, 即: -table的含义
    __tostring: 定义当table作为tostring()函式之参数被呼叫时的行为(例如: print(table)时将呼叫tostring(table)作为输出结果)
    __concat: 定义连接操作(".."运算符)
    __index: 定义当table中不存在的key值被试图获取时的行为
    __newindex: 定义在table中产生新key值时的行为

  • 相关阅读:
    execvp
    Linux系统调用getrlimit()与setrlimit()函数详解
    Va_start及Vsprintf应用
    waitpid()
    sdut 2408 Pick apples 夜
    poj 1273 Drainage Ditches 夜
    poj 1408 Fishnet 夜
    poj 1113 Wall 夜
    poj 1584 A Round Peg in a Ground Hole 夜
    poj 3007 Organize Your Train part II 夜
  • 原文地址:https://www.cnblogs.com/byfei/p/3112354.html
Copyright © 2020-2023  润新知