• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            的筆記

            隨時隨地編輯

            Metatables

            參考資料

            Referenced Values, Metatables, and Simple Inheritance>

            http://phrogz.net/lua/LearningLua_ValuesAndMetatables.html

            Metatable In Lua 淺嘗輒止
            http://www.cnblogs.com/simonw/archive/2007/01/17/622032.html

            扯淡

            metatable只是一個普通的table,當(dāng)你它他當(dāng)做metatable來用的時候,他就成了metatable:
               setmetatable( theFirstTable, theOtherTable )

            如果一個table有一個metatable,碰巧的是這個metatable有一寫特殊的關(guān)鍵字,則在使用這個table的時候,會發(fā)生一個寫有趣的事情:
             function gettable_event (table, key)
               local h
               if type(table) == "table" then
                 local v = rawget(table, key)
                 if v ~= nil then return v end
                 h = metatable(table).__index
                 if h == nil then return nil end
               else
                 h = metatable(table).__index
                 if h == nil then
                   error("...");
                 end
               end
               if type(h) == "function" then
                 return h(table, key)      -- call the handler
               else return h[key]          -- or repeat operation on it
            end

                也就是,你訪問表,其實最終訪問到表的metatable上去了。這些就是lua mirror a cplusplus class的基本原理。細節(jié)是復(fù)雜的,也很晦澀。


            Metatables

            15個重載方法(http://www.lua.org/manual/5.1/manual.html#2.8)

            We call the keys in a metatable events and the values metamethods. In the previous example, the event is "add" and the metamethod is the function that performs the addition.

            1. "add": the + operation.
            2. "sub": the - operation. Behavior similar to the "add" operation.
            3. "mul": the * operation. Behavior similar to the "add" operation.
            4. "div": the / operation. Behavior similar to the "add" operation.
            5. "mod": the % operation. Behavior similar to the "add" operation, with the operation o1 - floor(o1/o2)*o2 as the primitive operation.
            6. "pow": the ^ (exponentiation) operation. Behavior similar to the "add" operation, with the function pow (from the C math library) as the primitive operation."unm": the unary - operation.
            7. "concat": the .. (concatenation) operation.
            8. "len": the # operation.
            9. "eq": the == operation. The function getcomphandler defines how Lua chooses a metamethod for comparison operators. A metamethod only is selected when both
            10. objects being compared have the same type and the same metamethod for the selected operation.
            11. "lt": the < operation.
            12. "le": the <= operation.
            13. "index": The indexing access table[key].
            14. "newindex": The indexing assignment table[key] = value.
            15. "call": called when Lua calls a value.

            posted on 2011-07-02 20:49 的筆記 閱讀(293) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            国产精品久久久久影视不卡| 综合久久精品色| 丰满少妇人妻久久久久久4| 国产午夜精品久久久久九九| 久久精品国产亚洲Aⅴ蜜臀色欲 | 国产精品久久久久久久久免费| 国产精品久久永久免费| 四虎国产精品成人免费久久| 久久午夜无码鲁丝片| 国产精品青草久久久久福利99| 久久亚洲中文字幕精品一区| 久久综合丁香激情久久| 精品久久久久久无码不卡| 亚洲国产天堂久久综合网站 | 亚洲午夜无码久久久久小说| 精品国产VA久久久久久久冰 | 久久久国产精品网站| 国产色综合久久无码有码| 国产精品VIDEOSSEX久久发布| 麻豆亚洲AV永久无码精品久久| 久久精品免费大片国产大片| 久久97精品久久久久久久不卡| 亚洲欧美日韩久久精品第一区| 久久久久久久综合日本| 99久久国产综合精品网成人影院| 久久青青草原精品国产| 97精品依人久久久大香线蕉97| 午夜精品久久久久久影视777| 国产激情久久久久影院老熟女| 久久国产精品久久国产精品| 久久大香香蕉国产| 久久精品国产久精国产思思| 亚洲精品乱码久久久久久蜜桃不卡| 久久久网中文字幕| 久久久亚洲精品蜜桃臀| 亚洲国产高清精品线久久| 精品久久人人做人人爽综合 | 69久久精品无码一区二区| 久久精品国产亚洲AV嫖农村妇女| 久久久久久久久久久久中文字幕| 欧美一区二区三区久久综合|