{"id":548,"date":"2025-05-28T00:43:05","date_gmt":"2025-05-27T16:43:05","guid":{"rendered":"https:\/\/reverieland.cn\/?p=548"},"modified":"2025-06-11T00:52:27","modified_gmt":"2025-06-10T16:52:27","slug":"lua%e7%9f%a5%e8%af%86%e7%82%b9%e5%a4%8d%e4%b9%a0","status":"publish","type":"post","link":"https:\/\/reverieland.cn\/index.php\/548\/","title":{"rendered":"LUA\u77e5\u8bc6\u70b9\u590d\u4e60\uff08\u4e0a\uff09"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">0.\u521b\u5efa\u8868\u4e0e\u521b\u5efa\u5143\u8868<\/h2>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"lua\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">\n-- \u521b\u5efa\u8868\n\nlocal myTable = {}         -- \u521b\u5efa\u4e00\u4e2a\u7a7a\u8868\nmyTable.name = \"Lua\"       -- \u7ed9\u8868\u6dfb\u52a0\u5b57\u6bb5\nmyTable[1] = 100           -- \u4f7f\u7528\u6570\u7ec4\u7d22\u5f15\u6dfb\u52a0\u5143\u7d20\n\n-- \u521b\u5efa\u5143\u88681\n\nlocal t = {}  -- \u666e\u901a\u8868\nlocal mt = {} -- \u5143\u8868\nsetmetatable(t, mt) -- \u5c06\u5143\u8868 mt \u8d4b\u7ed9 t\n\n-- \u521b\u5efa\u5143\u88682\n\nlocal table = setmetatable({}, {}) \n<\/code><\/pre>\n\n\n\n<p>\u5143\u8868\u7684 key\/value \u53ef\u4ee5\u968f\u4fbf\u5199\uff0c\u548c\u8868\u7684key\u3001value\u6ca1\u6709\u76f4\u63a5\u8054\u7cfb\u3002<strong>\u53ea\u6709\u7279\u5b9a\u7684\u5143\u8868key\uff08\u5982 <code>__index<\/code>\uff09\u4f1a\u88ab Lua \u7528\u4e8e\u63a7\u5236\u884c\u4e3a\u3002<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1.ipairs\u548cpairs\u7684\u533a\u522b<\/strong><\/h2>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"lua\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">local tbl3 = {2, s1 = \"s1\", s2 = \"s2\", s3 = \"s3\" }\n\nprint(\"=======ipairs\u7684\u6267\u884c\u7ed3\u679c=======\")\nfor i, v in ipairs(tbl3) do\n    print(i, '=\", v)\nend\nprint(\"=======pairs\u7684\u6267\u884c\u7ed3\u679c=======\")\nfor i, v in pairs(tbl3) do\n    print(i, '=\", v)\nend<\/code><\/pre>\n\n\n\n<p><strong>\u7ed3\u8bba\uff1a\u5982\u679c\u662f\u6b63\u5e38\u7684\u6570\u7ec4\uff0cipair\u548cpairs\u7684\u7ed3\u679c\u6ca1\u6709\u533a\u522b<\/strong><br><strong>\u904d\u5386table\u6216array\u65f6\uff0c\u5982\u679ckey\u662f\u975e\u6570\u5b57\uff08\u5982\uff0c\u81ea\u5b9a\u4e49\u7684key\u53ef\u80fd\u4e3astring\uff09\uff0c\u4f7f\u7528pairs\u8fed\u4ee3\u904d\u5386\uff0c\u4f7f\u7528ipairs\u7684\u8bdd\u904d\u5386\u7ed3\u679c\u4e0d\u5168\u3001\u88ab\u622a\u65ad\u3002<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.\u51fd\u6570\u5192\u53f7\u548c\u70b9\u7684\u533a\u522b<\/h2>\n\n\n\n<p id=\"ua25893ea\"><strong>\u5192\u53f7\u5b9a\u4e49\u65b9\u6cd5\uff0c\u9ed8\u8ba4\u4f1a\u63a5\u53d7self\u53c2\u6570\uff1b\u800c\u70b9\u53f7\u5b9a\u4e49\u7684\u65f6\u5019\uff0c\u9ed8\u8ba4\u4e0d\u4f1a\u63a5\u53d7self\u53c2\u6570<\/strong>\u3002<\/p>\n\n\n\n<p id=\"ua25893ea\">class\u662f\u4e00\u4e2a\u8868\uff0ctest\u662f\u8868\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"lua\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">function class:test()\n    --\u8fd9\u91cc\u4f1a\u63a5\u53d7self\u53c2\u6570\uff0c\u6bd4\u5982\n    print(self.a, self.b)\n    --\u5728\u8fd9\u91ccself\u5c31\u662fclass\u5bf9\u8c61\u672c\u8eab\uff0c\u56e0\u6b64\u4e0d\u4f1a\u51fa\u9519\nend<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u8fd9\u91cc\u6362\u6210class.test()\uff0c\u5c31\u4e0d\u80fdprint(self.a, self.b)\uff0c\u5e94\u8be5\u624b\u52a8\u4f20\u5165\u6211\u4eec\u8981\u8c03\u7528\u7684\u4e1c\u897f\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"lua\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">local class = {}\n\nclass.a = 1\nclass.b = 2\n\nfunction class.test(tbl)\n    print(tbl.a, tbl.b)\nend\n\n-- \u8c03\u7528\u793a\u4f8b\nclass.test(class) -- \u8f93\u51fa\uff1a1  2<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3.\u53ef\u53d8\u53c2\u6570\uff08...\uff09<\/h2>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"php\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">-- \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570 average\uff0c\u63a5\u53d7\u53ef\u53d8\u53c2\u6570\uff08...\uff09\nfunction average(...)\n    local result = 0  -- \u7528\u4e8e\u7d2f\u52a0\u6240\u6709\u53c2\u6570\u7684\u603b\u548c\n    \n    -- \u5c06\u6240\u6709\u53ef\u53d8\u53c2\u6570\u6253\u5305\u6210\u4e00\u4e2a\u8868 args\uff0c\u4fbf\u4e8e\u904d\u5386\n    local args = {...}\n    \n    -- \u904d\u5386 args \u8868\u4e2d\u7684\u6bcf\u4e2a\u503c\n    for i, v in ipairs(args) do\n        result = result + v  -- \u5c06\u6bcf\u4e2a\u6570\u52a0\u5230 result \u4e2d\n    end\n    \n    -- \u8f93\u51fa\u603b\u5171\u4f20\u5165\u4e86\u591a\u5c11\u4e2a\u53c2\u6570\n    print(\"\u603b\u5171\u4f20\u5165: \" .. #args .. \"\u4e2a\u6570\")\n    \n    -- \u8fd4\u56de\u5e73\u5747\u503c\uff1a\u603b\u548c\u9664\u4ee5\u6570\u91cf\n    return result \/ #args\nend<\/code><\/pre>\n\n\n\n<p><br>Lua\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u53ef\u53d8\u6570\u76ee\u7684\u53c2\u6570\uff0c\u548cC++\u8bed\u8a00\u7c7b\u4f3c\uff1a\u5728\u51fd\u6570\u53c2\u6570\u5217\u8868\u4e2d\u4f7f\u7528\u4e09\u70b9\uff08...\uff09\u8868\u793a\u51fd\u6570\u6709\u53ef\u53d8\u7684\u53c2\u6570\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4.\u8003\u9898\uff1a\u5728\u5143\u8868\u4e2d\u7981\u6b62\u521b\u5efa\u65b0\u5b57\u6bb5\u5982\u4f55\u5199\uff1f<\/h2>\n\n\n\n<p>\u5e94\u7528\u573a\u666f\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"lua\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">\nlocal table = setmetatable({}, {}) \ntable.key = \"iam key\" \ntable.value = 123 \nprint(table.key)\n<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528 <code>setmetatable({}, {})<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a\u7a7a\u8868 <code>table<\/code>\uff0c\u5e76\u8d4b\u4e88\u5b83\u4e00\u4e2a\u7a7a\u7684\u5143\u8868\uff08\u6b64\u65f6\u6ca1\u6709\u4efb\u4f55\u5143\u65b9\u6cd5\uff09\u3002<\/p>\n\n\n\n<p>\u7136\u540e\u5411 <code>table<\/code> \u6dfb\u52a0\u4e24\u4e2a\u5b57\u6bb5\uff1a<code>key<\/code> \u548c <code>value<\/code>\u3002<\/p>\n\n\n\n<p>\u63a5\u7740\u6253\u5370 <code>table.key<\/code>\uff0c\u8f93\u51fa\u5e94\u4e3a <code>\"iam key\"<\/code>\u3002<\/p>\n\n\n\n<p>\u7b54\u6848\uff1a\u8003\u5bdf<strong>Lua \u4e2d\u7684\u5143\u8868\uff08metatable\uff09\u673a\u5236<\/strong>\uff0c\u5c24\u5176\u662f\u5143\u65b9\u6cd5 <code>__newindex<\/code> \u7684\u4f7f\u7528\u65b9\u5f0f\uff0c\u4ee5\u53ca\u5176\u5bf9 table \u8d4b\u503c\u884c\u4e3a\u7684\u5f71\u54cd\u3002<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"lua\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">local mt = getmetatable(table) -- \u83b7\u53d6table\u7684\u5143\u8868\nfunction mt:__newindex(key, value) -- \u5b9a\u4e49__newindex\u5143\u65b9\u6cd5\n\tmt[key] = nil\n\tprint(\"cannot create new property:\" .. key .. \",value=\" .. value)\nend<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>getmetatable(table)<\/code> \u62ff\u5230\u7684\u662f <code>table<\/code> \u7684\u5143\u8868\uff08\u4e4b\u524d\u8bbe\u5b9a\u4e3a <code>{}<\/code>\uff0c\u6240\u4ee5\u6b64\u5904\u80fd\u53d6\u5230\uff09\u3002<\/li>\n\n\n\n<li>\u5b9a\u4e49\u4e86 <code>__newindex<\/code> \u5143\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u5143\u65b9\u6cd5\u4f1a\u5728 <strong>\u5c1d\u8bd5\u7ed9 table \u6dfb\u52a0\u4e0d\u5b58\u5728\u7684\u952e\u65f6\u88ab\u8c03\u7528<\/strong>\u3002<\/li>\n\n\n\n<li>\u5728 <code>__newindex<\/code> \u4e2d\uff1a\n<ul class=\"wp-block-list\">\n<li>\u6ca1\u6709\u5b9e\u9645\u8d4b\u503c\uff08\u7528 <code>mt[key] = nil<\/code> \u53ea\u662f\u5728\u5143\u8868\u4e0a\u505a\u4e8b\uff0c\u5176\u5b9e\u6ca1\u610f\u4e49\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u9632\u6b62\u6c61\u67d3\uff09\u3002<\/li>\n\n\n\n<li>\u8f93\u51fa\u4e00\u4e2a\u9519\u8bef\u63d0\u793a\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u5173\u4e8e\u5143\u65b9\u6cd5<\/h3>\n\n\n\n<p><strong>\u5143\u65b9\u6cd5\u662f\u5199\u5728\u5143\u8868\u4e2d\u7684\u7279\u5b9a\u952e\u540d\u7684\u51fd\u6570\uff0cLua \u4f1a\u5728\u7279\u5b9a\u884c\u4e3a\u65f6\u81ea\u52a8\u8c03\u7528\u3002<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5143\u65b9\u6cd5<\/th><th>\u89e6\u53d1\u65f6\u673a<\/th><\/tr><\/thead><tbody><tr><td><code>__index<\/code><\/td><td>\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e<\/td><\/tr><tr><td><code>__newindex<\/code><\/td><td>\u8d4b\u503c\u7ed9\u4e0d\u5b58\u5728\u7684\u952e<\/td><\/tr><tr><td><code>__add<\/code><\/td><td>\u52a0\u6cd5\u8fd0\u7b97 <code>+<\/code><\/td><\/tr><tr><td><code>__tostring<\/code><\/td><td>\u4f7f\u7528 <code>tostring()<\/code> \u65f6<\/td><\/tr><tr><td><code>__call<\/code><\/td><td>\u8868\u5f53\u51fd\u6570\u8c03\u7528\u65f6<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">5. Lua\u4e2d\u7684\u6df1\u62f7\u8d1d\u4e0e\u6d45\u62f7\u8d1d<\/h2>\n\n\n\n<p><strong>1.\u62f7\u8d1d\u5bf9\u8c61\u662fstring\u3001number\u3001bool\u57fa\u672c\u7c7b\u578b\uff1a<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u6df1\u62f7\u8d1d<\/mark><\/strong><\/p>\n\n\n\n<p id=\"uf5ee1c15\">\u62f7\u2ec9\u7684\u8fc7\u7a0b\u5c31\u662f\u590d\u5236\u9ecf\u8d34\uff01\u4fee\u6539\u65b0\u62f7\u2ec9\u51fa\u6765\u7684\u5bf9\u8c61\uff0c\u4e0d \u4f1a\u5f71\u54cd\u539f\u5148\u5bf9\u8c61\u7684\u503c\uff0c\u4e24\u8005\u4e92\u4e0d\u2f32\u6d89\u3002<\/p>\n\n\n\n<p><strong>2.\u62f7\u8d1d\u5bf9\u8c61\u662ftable\u8868\uff1a<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u6d45\u62f7\u8d1d<\/mark><\/strong><\/p>\n\n\n\n<p id=\"ud1f8f399\">\u62f7\u2ec9\u51fa\u6765\u7684\u5bf9\u8c61\u548c\u539f\u5148\u5bf9\u8c61\u65f6\u540c\u2f00\u4e2a\u5bf9\u8c61\uff0c\u5360\u2f64\u540c\u2f00\u4e2a\u5bf9\u8c61\uff0c\u53ea\u662f\u2f00\u4e2a\u2f08\u7684\u4e24\u4e2a\u540d\u5b57\uff0c\u7c7b\u4f3cC#\u5f15\u2f64\u5730\u5740\uff0c\u6307\u5411\u540c\u2f00\u4e2a\u5806\u2fa5\u7684\u6570\u636e~\uff0c\u4e24\u8005\u4efb\u610f\u6539\u53d8\u90fd\u4f1a\u5f71\u54cd\u5bf9\u65b9\u3002<\/p>\n\n\n\n<p>\u5982\u4f55\u5bf9LUA\u7684\u8868\u5b9e\u73b0\u6df1\u62f7\u8d1d\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"lua\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">\n-- \u5b9e\u73b0\u4e00\u4e2a\u6df1\u62f7\u8d1d\u51fd\u6570\uff0c\u53ef\u4ee5\u590d\u5236\u4efb\u610f\u5d4c\u5957\u7684\u8868\u7ed3\u6784\uff0c\u5e76\u6b63\u786e\u5904\u7406\u5faa\u73af\u5f15\u7528\nfunction deepCopy(object)\n  -- \u521b\u5efa\u4e00\u4e2a\u67e5\u627e\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u7ecf\u62f7\u8d1d\u8fc7\u7684\u8868\uff0c\u4ee5\u5904\u7406\u5faa\u73af\u5f15\u7528\n  local lookup_table = {}\n\n  -- \u5b9a\u4e49\u4e00\u4e2a\u5c40\u90e8\u7684\u9012\u5f52\u62f7\u8d1d\u51fd\u6570\n  local function _copy(object)\n    -- \u5982\u679c\u4e0d\u662f\u8868\uff08\u5982 number\u3001string\u3001boolean\u3001nil\uff09\uff0c\u76f4\u63a5\u8fd4\u56de\u539f\u503c\n    if type(object) ~= \"table\" then\n      return object\n    -- \u5982\u679c\u8fd9\u4e2a\u8868\u4e4b\u524d\u5df2\u7ecf\u62f7\u8d1d\u8fc7\uff0c\u76f4\u63a5\u8fd4\u56de\u65e7\u62f7\u8d1d\uff0c\u907f\u514d\u6b7b\u5faa\u73af\n    elseif lookup_table[object] then\n      return lookup_table[object]\n    end\n\n    -- \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7a7a\u8868\uff0c\u7528\u6765\u5b58\u653e\u62f7\u8d1d\u7ed3\u679c\n    local new_table = {}\n    -- \u5c06\u539f\u59cb\u8868\u548c\u65b0\u8868\u7684\u6620\u5c04\u5173\u7cfb\u8bb0\u5f55\u4e0b\u6765\uff08\u9632\u6b62\u9012\u5f52\u5faa\u73af\uff09\n    lookup_table[object] = new_table\n\n    -- \u904d\u5386\u539f\u59cb\u8868\u4e2d\u7684\u6240\u6709\u952e\u503c\u5bf9\uff0c\u9012\u5f52\u590d\u5236\n    for key, value in pairs(object) do\n      -- \u62f7\u8d1d\u952e\u548c\u503c\uff0c\u652f\u6301\u952e\u4e5f\u4e3a table \u7684\u60c5\u51b5\n      new_table[_copy(key)] = _copy(value)\n    end\n\n    -- \u5c06\u539f\u8868\u7684\u5143\u8868\u4e5f\u62f7\u8d1d\u8fc7\u53bb\uff08\u5982\u679c\u6709\u7684\u8bdd\uff09\n    return setmetatable(new_table, getmetatable(object))\n  end\n\n  -- \u8fd4\u56de\u6700\u7ec8\u7684\u62f7\u8d1d\u7ed3\u679c\n  return _copy(object)\nend\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">6. Lua\u4e2d\u5b9e\u73b0\u9762\u5411\u5bf9\u8c61\u7684\u529f\u80fd<\/h2>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"lua\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">\n-- \u58f0\u660e\u4e00\u4e2a Lua \u7c7b\u7cfb\u7edf\u7684\u51fd\u6570\n-- \u53c2\u6570\uff1a\n--   className\uff1a\u7c7b\u540d\u5b57\u7b26\u4e32\uff0c\u4ec5\u7528\u4e8e\u6807\u8bc6\u7528\u9014\n--   super\uff1a\u7236\u7c7b\uff08\u53ef\u9009\uff09\uff0c\u7528\u4e8e\u5b9e\u73b0\u7ee7\u627f\nlocal function class(className, super)\n  -- \u521b\u5efa\u4e00\u4e2a\u7c7b\u8868\uff0c\u8bb0\u5f55\u7c7b\u540d\u548c\u7236\u7c7b\n  local clazz = {\n    __cname = className,  -- \u7c7b\u540d\uff08\u7528\u4e8e\u8c03\u8bd5\u6216\u6807\u8bc6\uff09\n    super = super         -- \u7236\u7c7b\u5f15\u7528\uff08\u53ef\u4e3a nil\uff09\n  }\n\n  -- \u5982\u679c\u6709\u7236\u7c7b\uff0c\u5219\u8bbe\u7f6e clazz \u7684\u5143\u8868\uff0c\u4f7f\u5176\u652f\u6301\u7ee7\u627f\n  if super then\n    setmetatable(clazz, {\n      __index = super  -- \u5f53\u8bbf\u95ee clazz \u4e2d\u4e0d\u5b58\u5728\u7684\u5b57\u6bb5\u65f6\uff0c\u8f6c\u800c\u4ece super \u67e5\u627e\n    })\n  end\n\n  -- \u5b9a\u4e49\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\uff0c\u7528\u4e8e\u521b\u5efa\u7c7b\u5b9e\u4f8b\n  clazz.new = function(...)\n    -- \u521b\u5efa\u4e00\u4e2a\u5b9e\u4f8b\u8868\n    local instance = {}\n\n    -- \u8bbe\u7f6e\u5143\u8868\uff0c\u8ba9 instance \u53ef\u4ee5\u8bbf\u95ee clazz \u7684\u65b9\u6cd5\u548c\u5b57\u6bb5\n    setmetatable(instance, {\n      __index = clazz  -- \u5b9e\u73b0\u5bf9\u8c61\u5bf9\u7c7b\u65b9\u6cd5\u7684\u8bbf\u95ee\n    })\n\n    -- \u5982\u679c\u7c7b\u4e2d\u5b9a\u4e49\u4e86\u6784\u9020\u51fd\u6570 ctor\uff0c\u5219\u8c03\u7528\u5b83\n    if clazz.ctor then\n      clazz.ctor(instance, ...)  -- \u4ee5 instance \u4f5c\u4e3a self \u4f20\u5165\n    end\n\n    return instance  -- \u8fd4\u56de\u6784\u5efa\u597d\u7684\u5b9e\u4f8b\n  end\n\n  return clazz  -- \u8fd4\u56de\u5b9a\u4e49\u597d\u7684\u7c7b\nend\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u529f\u80fd\u9879<\/th><th>\u5b9e\u73b0\u65b9\u5f0f<\/th><\/tr><\/thead><tbody><tr><td>\u7c7b\u7684\u5b9a\u4e49<\/td><td><code>local clazz = {}<\/code><\/td><\/tr><tr><td>\u7c7b\u7684\u7ee7\u627f<\/td><td>\u901a\u8fc7\u8bbe\u7f6e\u5143\u8868\uff1a<code>setmetatable(clazz, { __index = super })<\/code><\/td><\/tr><tr><td>\u5b9e\u4f8b\u5316\u5bf9\u8c61<\/td><td><code>clazz.new(...)<\/code> \u521b\u5efa\u5bf9\u8c61\u5e76\u7ed1\u5b9a\u5143\u8868<\/td><\/tr><tr><td>\u6784\u9020\u51fd\u6570\u652f\u6301<\/td><td>\u8c03\u7528 <code>clazz.ctor(instance, ...)<\/code><\/td><\/tr><tr><td>\u5bf9\u8c61\u8bbf\u95ee\u65b9\u6cd5<\/td><td><code>setmetatable(instance, { __index = clazz })<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u4f7f\u7528\u6848\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"lua\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">\n-- \u5b9a\u4e49\u4e00\u4e2a\u57fa\u7c7b\nlocal Animal = class(\"Animal\")\nfunction Animal:ctor(name)\n  self.name = name\nend\nfunction Animal:speak()\n  print(self.name .. \" makes a sound.\")\nend\n\n-- \u5b9a\u4e49\u4e00\u4e2a\u5b50\u7c7b\nlocal Dog = class(\"Dog\", Animal)\nfunction Dog:ctor(name)\n  self.super.ctor(self, name) -- \u8c03\u7528\u7236\u7c7b\u6784\u9020\nend\nfunction Dog:speak()\n  print(self.name .. \" barks.\")\nend\n\n-- \u4f7f\u7528\nlocal d = Dog.new(\"Buddy\")\nd:speak()  -- \u8f93\u51fa: Buddy barks.\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">7. Upvalue<\/h2>\n\n\n\n<p>\u5728 Lua \u4e2d\uff0c<strong>upvalue\uff08\u4e0a\u503c\uff09<\/strong> \u662f\u6307\u5728\u4e00\u4e2a<strong>\u5d4c\u5957\u51fd\u6570\uff08\u95ed\u5305\uff09\u4e2d\u5f15\u7528\u5176\u5916\u90e8\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf<\/strong>\u3002upvalue \u7684\u4f5c\u7528\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4fdd\u6301\u72b6\u6001<\/strong><br>\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\uff0cupvalue \u4ecd\u7136\u4f1a\u88ab\u95ed\u5305\u6301\u6709\uff0c\u4ece\u800c\u4fdd\u7559\u53d8\u91cf\u72b6\u6001\u3002<\/li>\n\n\n\n<li><strong>\u521b\u5efa\u95ed\u5305\uff08Closure\uff09<\/strong><br>Lua \u7684\u95ed\u5305\u4f9d\u8d56 upvalue\uff0c\u95ed\u5305\u53ef\u4ee5\u6355\u83b7\u5176\u5916\u90e8\u4f5c\u7528\u57df\u7684\u5c40\u90e8\u53d8\u91cf\u3002<\/li>\n\n\n\n<li><strong>\u5171\u4eab\u53d8\u91cf<\/strong><br>\u591a\u4e2a\u95ed\u5305\u5f15\u7528\u540c\u4e00\u4e2a upvalue \u65f6\uff0c\u5b83\u4eec\u5171\u4eab\u8fd9\u4e2a\u53d8\u91cf\u7684\u503c\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a\u4e0b\u9762\u7684\u4ee3\u7801\u8fd4\u56de\u4e86\u4e24\u4e2a\u533f\u540d\u51fd\u6570\uff1a\u8fd9\u4e24\u4e2a\u533f\u540d\u51fd\u6570\u90fd\u662f\u95ed\u5305\uff0c\u5f15\u7528\u5e76\u4fee\u6539count\u3002<\/p>\n\n\n\n<p>\u56e0\u4e3a\u8fd9\u4e24\u4e2a\u51fd\u6570\u90fd\u662f\u5728\u540c\u4e00\u4e2a\u4f5c\u7528\u57df\u4e2d\u521b\u5efa\u7684\uff0c\u5b83\u4eec <strong>\u5171\u4eab\u540c\u4e00\u4e2a <code>count<\/code> upvalue<\/strong>\u3002<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"lua\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">\nfunction createCounters()\n  local count = 0  -- \u5c40\u90e8\u53d8\u91cf count \u662f upvalue\n  return function() \n    count = count + 1\n    return count\n  end,\n  function() \n    count = count - 1\n    return count\n  end\nend\n<\/code><\/pre>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"lua\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"12\" data-enlighter-highlight=\"\">\nlocal incCounter, decCounter = createCounters()\n\nprint(incCounter()) -- \u7b2c\u4e00\u6b21\u52a0 1\uff0ccount \u4ece 0 \u2192 1\uff0c\u8f93\u51fa 1\nprint(incCounter()) -- \u518d\u52a0 1\uff0ccount \u4ece 1 \u2192 2\uff0c\u8f93\u51fa 2\nprint(decCounter()) -- \u51cf 1\uff0ccount \u4ece 2 \u2192 1\uff0c\u8f93\u51fa 1\nprint(decCounter()) -- \u518d\u51cf 1\uff0ccount \u4ece 1 \u2192 0\uff0c\u8f93\u51fa 0\n<\/code><\/pre>\n\n\n\n<p>createCounters\u662f\u591a\u8fd4\u56de\u503c\u51fd\u6570\uff0c\u8fd9\u91cc\u5206\u522b\u4f7f\u7528incCounter\u548cdecCounter\u6765\u63a5\u6536\u7b2c\u4e00\u4e2a\u3001\u7b2c\u4e8c\u4e2a\u8fd4\u56de\u503c\u3002<\/p>\n\n\n\n<p>\u4ec5\u4f7f\u7528incCounter\uff0c\u8868\u793a\u4f60<strong>\u53ea\u63a5\u6536\u4e86 <code>createCounters<\/code> \u8fd4\u56de\u7684\u7b2c\u4e00\u4e2a\u51fd\u6570<\/strong>\uff0c\u4e5f\u5c31\u662f\u9012\u589e\u7684\u90a3\u4e2a\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">upvalue\u7684\u5b58\u50a8\u7ec6\u8282<\/h3>\n\n\n\n<p><br>\u5728lua\u4e2d\uff0c\u4f1a\u751f\u6210\u4e00\u4e2a\u5168\u5c40\u6808\uff0c\u6240\u6709\u7684upvalue\u90fd\u4f1a\u6307\u5411\u8be5\u6808\u4e2d\u7684\u503c\uff0c\u82e5\u5bf9\u5e94\u7684\u53c2\u6570\u79bb\u5f00\u7684\u4f5c\u7528\u57df\uff0c\u6808\u4e2d\u7684\u503c\u4e5f\u4f1a\u88ab\u91ca\u653e\uff0cupvalue\u7684\u6307\u9488\u4f1a\u6307\u5411\u81ea\u5df1\uff0c\u7b49\u5f85\u88abgc<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u95ed\u5305\u521b\u5efa\u9636\u6bb5<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53\u5916\u5c42\u51fd\u6570\u6267\u884c\u5230\u5185\u5c42\u51fd\u6570\u5b9a\u4e49\u7684\u4f4d\u7f6e\u65f6\uff0cLua \u4f1a\u5206\u6790\u5185\u5c42\u51fd\u6570\u4f9d\u8d56\u7684\u5916\u90e8\u53d8\u91cf\uff08upvalue\uff09\uff0c\u5e76\u4e3a\u8fd9\u4e9b\u53d8\u91cf\u751f\u6210\u4e00\u4e2a\u9759\u6001\u7684\u5f15\u7528\u94fe\uff08\u5982 upvalue \u94fe\u8868\uff09\u3002<\/li>\n\n\n\n<li>\u8fd9\u4e9b\u5f15\u7528\u4f1a\u88ab\u56fa\u5316\u5728\u95ed\u5305\u7684\u5185\u5b58\u7ed3\u6784\u4e2d\uff0c\u5f62\u6210\u7c7b\u4f3c\u201c\u5feb\u7167\u201d\u7684\u6548\u679c\u3002\u5373\u4f7f\u5916\u5c42\u51fd\u6570\u7684\u6267\u884c\u5df2\u7ed3\u675f\uff0c\u95ed\u5305\u4ecd\u80fd\u901a\u8fc7\u8be5\u5f15\u7528\u94fe\u5b89\u5168\u5730\u8bbf\u95ee\u6240\u9700\u7684\u5916\u90e8\u53d8\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u95ed\u5305\u8c03\u7528\u9636\u6bb5<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6bcf\u6b21\u8c03\u7528\u95ed\u5305\u65f6\uff0c\u76f4\u63a5\u901a\u8fc7\u9884\u5148\u751f\u6210\u7684\u5f15\u7528\u94fe\u8bbf\u95ee upvalue\uff0c\u65e0\u9700\u989d\u5916\u904d\u5386\u6216\u91cd\u5efa\u6307\u9488\u3002<\/li>\n\n\n\n<li>\u8fd9\u79cd\u8bbe\u8ba1\u4fdd\u8bc1\u4e86\u95ed\u5305\u7684\u9ad8\u6548\u6027\uff0c\u907f\u514d\u4e86\u56e0\u52a8\u6001\u67e5\u627e\u5bfc\u81f4\u7684\u6027\u80fd\u635f\u8017\u3002<\/li>\n<\/ul>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>\u5171\u4eab\u4e0e\u66f4\u65b0\u7279\u6027<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u82e5\u591a\u4e2a\u95ed\u5305\u5171\u4eab\u540c\u4e00 upvalue\uff08\u4f8b\u5982\u5d4c\u5957\u51fd\u6570\u5171\u4eab\u7236\u7ea7\u53d8\u91cf\uff09\uff0c\u5b83\u4eec\u7684\u5f15\u7528\u4f1a\u6307\u5411\u76f8\u540c\u7684\u5185\u5b58\u5730\u5740\uff0c\u786e\u4fdd\u6570\u636e\u4e00\u81f4\u6027\u3002<\/li>\n\n\n\n<li>\u4fee\u6539 upvalue \u4f1a\u5f71\u54cd\u6240\u6709\u5173\u8054\u5b83\u7684\u95ed\u5305\uff0c\u56e0\u4e3a\u64cd\u4f5c\u7684\u662f\u540c\u4e00\u5e95\u5c42\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>0.\u521b\u5efa\u8868\u4e0e\u521b\u5efa\u5143\u8868 \u5143\u8868\u7684 key\/value \u53ef\u4ee5\u968f\u4fbf\u5199\uff0c\u548c\u8868\u7684key\u3001value\u6ca1\u6709\u76f4\u63a5\u8054\u7cfb\u3002\u53ea\u6709\u7279\u5b9a\u7684\u5143\u8868key\uff08\u5982 __ &#8230;<\/p>\n","protected":false},"author":1,"featured_media":551,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[34],"tags":[36,35,41],"class_list":["post-548","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c","tag-c","tag-lua","tag-41"],"_links":{"self":[{"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/posts\/548","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/comments?post=548"}],"version-history":[{"count":33,"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/posts\/548\/revisions"}],"predecessor-version":[{"id":598,"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/posts\/548\/revisions\/598"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/media\/551"}],"wp:attachment":[{"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/media?parent=548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/categories?post=548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/reverieland.cn\/index.php\/wp-json\/wp\/v2\/tags?post=548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}