{"id":2329,"date":"2025-09-18T18:04:09","date_gmt":"2025-09-18T10:04:09","guid":{"rendered":"https:\/\/www.madbull.site\/?p=2329"},"modified":"2025-10-15T09:56:48","modified_gmt":"2025-10-15T01:56:48","slug":"lua%e4%b8%ad%ef%bc%8c%e8%a1%a8%e3%80%81%e5%85%83%e8%a1%a8%e3%80%81%e5%af%b9%e8%b1%a1%e3%80%81%e7%b1%bb%e7%9a%84%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=2329","title":{"rendered":"Lua\u4e2d\uff0c\u8868\u3001\u5143\u8868\u3001\u5bf9\u8c61\u3001\u7c7b\u7684\u89e3\u6790"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\ud83e\uddf1 \u4e00\u3001\u8868\uff08table\uff09\u2014\u2014 Lua \u7684\u552f\u4e00\u590d\u5408\u6570\u636e\u7ed3\u6784<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 1.1 \u672c\u8d28<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Lua \u4e2d<strong>\u552f\u4e00\u7684\u5185\u7f6e\u590d\u5408\u6570\u636e\u7ed3\u6784<\/strong><\/li>\n\n\n\n<li>\u53ef\u4ee5\u540c\u65f6\u4f5c\u4e3a\uff1a\n<ul class=\"wp-block-list\">\n<li>\u6570\u7ec4\uff08array\uff09<\/li>\n\n\n\n<li>\u5b57\u5178\uff08map \/ hash \/ associative array\uff09<\/li>\n\n\n\n<li>\u5bf9\u8c61\uff08object\uff09<\/li>\n\n\n\n<li>\u6a21\u5757\uff08module\uff09<\/li>\n\n\n\n<li>\u7ed3\u6784\u4f53\uff08struct\uff09<\/li>\n\n\n\n<li>\u547d\u540d\u7a7a\u95f4\uff08namespace\uff09<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 1.2 \u57fa\u672c\u8bed\u6cd5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>local t = {\n    name = \"Alice\",           -- \u5b57\u7b26\u4e32\u952e\n    age = 25,                 -- \u5b57\u7b26\u4e32\u952e\uff0c\u7b49\u4ef7\u4e8e &#91;\"age\"] = 25\n    &#91;true] = \"yes\",           -- \u4efb\u610f\u7c7b\u578b\u952e\uff08\u9664 nil\uff09\n    &#91;2] = \"first\",            -- \u6570\u5b57\u952e\uff0c\u63d2\u5165\u5230\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\uff08\u7d22\u5f15\u4ece1\u5f00\u59cb\uff09\uff0c\u6b64\u4f4d\u7f6e\u5411\u4e0a\u81f32\u4f4d\u7f6e\u7684\u6570\u636e\uff0c\u79fb\u52a8\u5230\u6700\u672b\u5c3e\n    sayHello = function(self) -- \u65b9\u6cd5\n        print(\"Hello, I'm \" .. self.name)\n    end\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 1.3 \u64cd\u4f5c\u65b9\u5f0f<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>t.name = \"Bob\"                -- \u8bbe\u7f6e\u5b57\u6bb5\nprint(t.age)                  -- \u8bfb\u53d6\u5b57\u6bb5\nt:sayHello()                  -- \u8c03\u7528\u65b9\u6cd5\uff08: \u81ea\u52a8\u4f20 self\uff09\nt&#91;\"sayHello\"](t)              -- \u7b49\u4ef7\u5199\u6cd5\n\n<strong>for<\/strong> k, v <strong>in<\/strong> pairs(t) <strong>do<\/strong>       -- \u904d\u5386\n    print(k, v)\n<strong>end<\/strong><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 1.4 \u8868\u7684\u6838\u5fc3\u4f5c\u7528<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u5b58\u50a8\u6570\u636e + \u884c\u4e3a\uff08\u65b9\u6cd5\uff09 \u2192 \u6210\u4e3a\u201c\u5bf9\u8c61\u201d\u7684\u57fa\u7840<\/strong><\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde9 \u4e8c\u3001\u5143\u8868\uff08metatable\uff09\u2014\u2014 \u8868\u7684\u201c\u884c\u4e3a\u8bf4\u660e\u4e66\u201d<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 2.1 \u672c\u8d28<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5143\u8868\u672c\u8eab\u4e5f\u662f\u4e00\u4e2a\u8868<\/strong><\/li>\n\n\n\n<li>\u5b83\u5305\u542b\u201c\u5143\u65b9\u6cd5\uff08metamethods\uff09\u201d\uff0c\u7528\u4e8e\u5b9a\u4e49\u201c\u5f53\u5bf9\u539f\u8868\u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\u65f6\uff0c\u5e94\u8be5\u600e\u4e48\u505a\u201d<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 2.2 \u5982\u4f55\u8bbe\u7f6e\/\u83b7\u53d6<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>setmetatable(table, metatable)  -- \u8bbe\u7f6e\ngetmetatable(table)             -- \u83b7\u53d6<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u26a0\ufe0f \u53ea\u6709 <strong>table\u3001userdata\u3001thread<\/strong> \u53ef\u4ee5\u8bbe\u5143\u8868\uff08Lua 5.4+ \u652f\u6301\u66f4\u591a\u7c7b\u578b\uff0c\u4f46\u4e00\u822c\u53ea\u7528\u4e8e table\uff09<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 2.3 \u5e38\u7528\u5143\u65b9\u6cd5<\/h3>\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><th>\u7528\u9014<\/th><\/tr><\/thead><tbody><tr><td><code>__index<\/code><\/td><td><code>t[key]<\/code> \u4e14 key \u4e0d\u5b58\u5728<\/td><td>\u9ed8\u8ba4\u503c\u3001\u7ee7\u627f\u3001\u4ee3\u7406<\/td><\/tr><tr><td><code>__newindex<\/code><\/td><td><code>t[key] = value<\/code> \u4e14 key \u4e0d\u5b58\u5728<\/td><td>\u6743\u9650\u63a7\u5236\u3001\u65e5\u5fd7\u3001\u53ea\u8bfb\u8868<\/td><\/tr><tr><td><code>__add<\/code><\/td><td><code>a + b<\/code><\/td><td>\u81ea\u5b9a\u4e49\u52a0\u6cd5<\/td><\/tr><tr><td><code>__call<\/code><\/td><td><code>f(...)<\/code><\/td><td>\u8ba9\u8868\u50cf\u51fd\u6570\u4e00\u6837\u88ab\u8c03\u7528<\/td><\/tr><tr><td><code>__tostring<\/code><\/td><td><code>print(t)<\/code> \u6216 <code>tostring(t)<\/code><\/td><td>\u81ea\u5b9a\u4e49\u5b57\u7b26\u4e32\u8868\u793a<\/td><\/tr><tr><td><code>__len<\/code><\/td><td><code>#t<\/code><\/td><td>\u81ea\u5b9a\u4e49\u957f\u5ea6<\/td><\/tr><tr><td><code>__eq<\/code>, <code>__lt<\/code>, <code>__le<\/code><\/td><td><code>==<\/code>, <code>&lt;<\/code>, <code>&lt;=<\/code><\/td><td>\u81ea\u5b9a\u4e49\u6bd4\u8f83<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 2.4 \u793a\u4f8b\uff1a<code>__index<\/code> \u5b9e\u73b0\u9ed8\u8ba4\u503c<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>local<\/strong> defaults = { x = 0, y = 0, color = \"white\" }\n<strong>local<\/strong> obj = {}\nsetmetatable(obj, { __index = defaults })\n\nprint(obj.x)      -- 0 \uff08\u4ece defaults \u83b7\u53d6\uff09\nobj.x = 10\nprint(obj.x)      -- 10 \uff08\u73b0\u5728 obj \u81ea\u5df1\u6709 x\uff09<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 2.5 \u793a\u4f8b\uff1a<code>__call<\/code> \u5b9e\u73b0\u53ef\u8c03\u7528\u5bf9\u8c61<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>local<\/strong> counter = { count = 0 }\n<strong>local<\/strong> mt = {\n    __call = <strong>function<\/strong>(self, n)\n        self.count = self.count + (n <strong>or<\/strong> 1)\n        <strong>return<\/strong> self.count\n    <strong>end<\/strong>\n}\nsetmetatable(counter, mt)\n\nprint(counter())     -- 1\nprint(counter(5))    -- 6<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 2.6 \u5143\u8868\u7684\u6838\u5fc3\u4f5c\u7528<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u63a7\u5236\u8868\u7684\u884c\u4e3a \u2014\u2014 \u8fd0\u7b97\u7b26\u91cd\u8f7d\u3001\u5c5e\u6027\u4ee3\u7406\u3001\u51fd\u6570\u8c03\u7528\u3001\u7ee7\u627f\u673a\u5236\u7b49<\/strong><\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddcd \u4e09\u3001\u5bf9\u8c61\uff08object\uff09\u2014\u2014 \u7528\u8868\u6a21\u62df\u7684\u5bf9\u8c61<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 3.1 \u4ec0\u4e48\u662f\u5bf9\u8c61\uff1f<\/h3>\n\n\n\n<p>\u5728 Lua \u4e2d\uff0c<strong>\u5bf9\u8c61 = \u8868 + \u65b9\u6cd5 +\uff08\u53ef\u9009\uff09\u5143\u8868<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>local<\/strong> obj = {\n    x = 10,\n    y = 20,\n    move = <strong>function<\/strong>(self, dx, dy)\n        self.x = self.x + dx\n        self.y = self.y + dy\n    <strong>end<\/strong>\n}\n\nobj:move(5, 5)  -- : \u8bed\u6cd5\u7cd6\uff0c\u81ea\u52a8\u4f20 self\nprint(obj.x, obj.y)  -- 15  25<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 3.2 \u5bf9\u8c61\u7684\u6838\u5fc3\u7279\u5f81<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u72b6\u6001\uff08\u6570\u636e\uff09<\/strong>\uff1a<code>x<\/code>, <code>y<\/code><\/li>\n\n\n\n<li><strong>\u884c\u4e3a\uff08\u65b9\u6cd5\uff09<\/strong>\uff1a<code>move<\/code><\/li>\n\n\n\n<li><strong>\u6807\u8bc6\uff08identity\uff09<\/strong>\uff1a\u6bcf\u4e2a\u8868\u662f\u72ec\u7acb\u5bf9\u8c61<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 3.3 \u5982\u4f55\u201c\u6784\u9020\u201d\u5bf9\u8c61\uff1f<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u65b9\u5f0f 1\uff1a\u76f4\u63a5\u5b57\u9762\u91cf<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>local<\/strong> obj = { x = 10, y = 20 }<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u65b9\u5f0f 2\uff1a\u5de5\u5382\u51fd\u6570<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>local<\/strong> <strong>function<\/strong> <strong>newPoint<\/strong>(x, y)\n    <strong>return<\/strong> {\n        x = x <strong>or<\/strong> 0,\n        y = y <strong>or<\/strong> 0,\n        move = <strong>function<\/strong>(self, dx, dy)\n            self.x = self.x + dx\n            self.y = self.y + dy\n        <strong>end<\/strong>\n    }\n<strong>end<\/strong>\n\n<strong>local<\/strong> p = newPoint(10, 20)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u65b9\u5f0f 3\uff1a\u7528\u5143\u8868\u5b9e\u73b0\u201c\u7c7b\u5f0f\u201d\u6784\u9020\uff08\u89c1\u4e0b\u6587\uff09<\/h4>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udfdb \u56db\u3001\u7c7b\uff08class\uff09\u2014\u2014 \u7528\u8868 + \u5143\u8868\u6a21\u62df\u7684\u201c\u7c7b\u201d<\/h2>\n\n\n\n<p>Lua <strong>\u6ca1\u6709\u5185\u7f6e\u7c7b<\/strong>\uff0c\u4f46\u53ef\u4ee5\u7528\u201c\u539f\u578b\u7ee7\u627f\u201d\u6a21\u62df\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 4.1 \u57fa\u672c\u201c\u7c7b\u201d\u7ed3\u6784<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u201c\u7c7b\u201d\u5b9a\u4e49\uff08\u5176\u5b9e\u662f\u539f\u578b\u5bf9\u8c61\uff09\nlocal Point = {}\n\n-- \u5b9a\u4e49 __tostring \u884c\u4e3a\uff08\u6ce8\u610f\uff1a\u8fd9\u662f\u7ed9\u201c\u5b9e\u4f8b\u201d\u7528\u7684\uff0c\u4e0d\u662f\u7ed9\u7c7b\u672c\u8eab\uff09\nlocal mt = {\n    __tostring = function(self)\n        return string.format(\"Point(%d, %d)\", self.x, self.y)\n    end\n}\n\n-- \u6784\u9020\u51fd\u6570\nfunction Point:new(x, y)\n    local obj = {\n        x = x or 0,\n        y = y or 0\n    }\n    -- \u5173\u952e\uff1a\u8ba9\u5b9e\u4f8b\uff08obj\u8868\uff09\u7684\u5143\u8868\u8981\u5305\u542b __tostring \u548c __index\n    setmetatable(obj, {\n        __index = self,      -- \u521d\u59cb\u5316 __index \u4e3a Point \uff08\u7ee7\u627fPoint\u7684\u6210\u5458\uff09\u3002\n        __tostring = mt.__tostring  -- \u76f4\u63a5\u8bbe\u7f6e\u5143\u65b9\u6cd5\n    })\n    return obj\nend\n\n-- \u65b9\u6cd5\nfunction Point:move(dx, dy)\n    self.x = self.x + dx\n    self.y = self.y + dy\nend\n\n-- \u53ef\u9009\uff1a\u8ba9\u7c7b\u672c\u8eab\uff08Point\u8868\uff09\u4e5f\u652f\u6301 tostring\uff08\u8c03\u8bd5\u7528\uff09\nsetmetatable(Point, {\n    __tostring = function()\n        return \"Class Point\"\n    end,\n    __call = Point.new  -- \u652f\u6301 Point(10, 20) \u8bed\u6cd5\n})\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 4.2 \u4f7f\u7528\u201c\u7c7b\u201d<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u6d4b\u8bd5\nlocal p1 = Point:new(10, 20)\nprint(p1)           -- Point(10, 20)\np1:move(5, 5)\nprint(p1)           -- Point(15, 25)\n\nlocal p2 = Point(30, 40)  -- \u4f7f\u7528 __call\nprint(p2)           -- Point(30, 40)\n\nprint(Point)        -- Class Point \uff08\u53ef\u9009\u529f\u80fd\uff09<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 4.3 \u7ee7\u627f\u600e\u4e48\u5b9e\u73b0\uff1f<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u5b50\u7c7b\nlocal ColorPoint = Point:new()  -- \u521b\u5efa\u5b50\u7c7b\u539f\u578b\nColorPoint.color = \"red\"\n\n-- \u8bbe\u7f6e ColorPoint \uff0c\u4f7f\u7528 __index \u7ee7\u627f Point\nsetmetatable(ColorPoint, { __index = Point })\n\n-- \u5b9a\u4e49 __instance_mt\nColorPoint.__instance_mt = {\n    __index = function(self, key)\n        -- \u9996\u5148\u5728 ColorPoint \u4e2d\u67e5\u627e\uff0c\u628a\u6240\u6709 \u7236\u7c7b Point \u7684\u51fd\u6570\u5168\u90e8\u7ee7\u627f\u8fc7\u6765\u3002\n        local value = ColorPoint&#91;key]\n        if value ~= nil then\n            return value\n        end\n        -- \u5982\u679c\u6ca1\u627e\u5230\uff0c\u518d\u5230 Point \u4e2d\u67e5\u627e\n        return Point&#91;key]\n    end,\n    __tostring = function(self)\n        return string.format(\"ColorPoint(%d, %d, %s)\", self.x, self.y, self.color)\n    end\n}\n\n-- \u5b50\u7c7b\u6784\u9020\u51fd\u6570\nfunction ColorPoint:new(x, y, color)\n    local obj = Point:new(x, y)  -- \u8c03\u7528\u7236\u7c7b\u6784\u9020\u51fd\u6570\n    obj.color = color or self.color\n    -- \u5173\u952e\uff1a\u91cd\u65b0\u8bbe\u7f6e\u5143\u8868\u4e3a ColorPoint \u7684\u5b9e\u4f8b\u5143\u8868\n    setmetatable(obj, self.__instance_mt)\n    return obj\nend\n\n-- \u53ef\u9009\uff1a\u7ed9 ColorPoint \u7c7b\u672c\u8eab\u8bbe\u7f6e\u5143\u8868\nsetmetatable(ColorPoint, {\n    __tostring = function() return \"Class ColorPoint\" end,\n    __call = function(self, ...)\n        return self:new(...)\n    end  -- \u4fee\u6b63\uff1a\u4f7f\u7528\u533f\u540d\u51fd\u6570\u8c03\u7528 self:new(...)\n})\n\n\n-- \u5b50\u7c7b\u6d4b\u8bd5\nlocal cp = ColorPoint(50, 60, \"blue\")\nprint(cp)           -- ColorPoint(50, 60, blue) \u2190 \u7ec8\u4e8e\u6b63\u786e\u4e86\uff01\ncp:move(10, 10)\nprint(cp)           -- ColorPoint(60, 70, blue) \u2190 move \u7ee7\u627f\u81ea Point\uff01\n\nprint(ColorPoint)   -- Class ColorPoint<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 4.4 \u201c\u7c7b\u201d\u7684\u672c\u8d28<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u4e00\u4e2a\u201c\u539f\u578b\u8868\u201d + \u4e00\u4e2a\u5143\u8868\uff08\u7528 <code>__index<\/code> \u6307\u5411\u539f\u578b\u8868\uff09<\/strong><\/p>\n<\/blockquote>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5bf9\u8c61\u521b\u5efa\u65f6\uff0c\u628a\u81ea\u5df1\u7684\u5143\u8868\u7684 <code>__index<\/code> \u6307\u5411\u201c\u7c7b\u201d<\/li>\n\n\n\n<li>\u5f53\u8bbf\u95ee\u5bf9\u8c61\u4e0d\u5b58\u5728\u7684\u5b57\u6bb5\u65f6\uff0cLua \u4f1a\u53bb\u201c\u7c7b\u201d\u91cc\u627e \u2192 \u5b9e\u73b0\u201c\u65b9\u6cd5\u7ee7\u627f\u201d<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udcdc \u4e94\u3001\u4f7f\u7528\u89c4\u5219 &amp; \u6700\u4f73\u5b9e\u8df5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 5.1 \u8868\u7684\u4f7f\u7528\u89c4\u5219<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7528\u4f5c\u6570\u636e\u5bb9\u5668\u3001\u5bf9\u8c61\u3001\u6a21\u5757<\/li>\n\n\n\n<li>\u907f\u514d\u6df7\u7528\u6570\u7ec4\u548c\u5b57\u5178\u90e8\u5206\uff08\u9664\u975e\u5fc5\u8981\uff09<\/li>\n\n\n\n<li>\u952e\u5c3d\u91cf\u7528\u5b57\u7b26\u4e32\u6216\u6570\u5b57\uff08\u907f\u514d\u7528\u5e03\u5c14\u3001\u51fd\u6570\u3001\u8868\u4f5c\u952e\uff0c\u9664\u975e\u6709\u7279\u6b8a\u9700\u6c42\uff09<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>:<\/code> \u5b9a\u4e49\u548c\u8c03\u7528\u65b9\u6cd5\uff0c\u81ea\u52a8\u7ba1\u7406 <code>self<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 5.2 \u5143\u8868\u7684\u4f7f\u7528\u89c4\u5219<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u53ea\u7528\u4e8e\u5b9a\u4e49\u884c\u4e3a\uff0c\u4e0d\u7528\u4e8e\u5b58\u50a8\u4e1a\u52a1\u6570\u636e<\/strong><\/li>\n\n\n\n<li>\u591a\u4e2a\u5bf9\u8c61\u53ef\u5171\u4eab\u540c\u4e00\u4e2a\u5143\u8868\uff08\u8282\u7701\u5185\u5b58\uff09<\/li>\n\n\n\n<li><code>__index<\/code> \u6700\u5e38\u7528 \u2014\u2014 \u5b9e\u73b0\u7ee7\u627f\u3001\u9ed8\u8ba4\u503c\u3001\u4ee3\u7406<\/li>\n\n\n\n<li><code>__newindex<\/code> \u7528\u4e8e\u62e6\u622a\u5199\u64cd\u4f5c \u2014\u2014 \u5b9e\u73b0\u53ea\u8bfb\u8868\u3001\u65e5\u5fd7\u3001\u9a8c\u8bc1<\/li>\n\n\n\n<li><code>__call<\/code> \u8ba9\u5bf9\u8c61\/\u7c7b\u53ef\u8c03\u7528 \u2014\u2014 \u5b9e\u73b0\u5de5\u5382\u3001\u95ed\u5305<\/li>\n\n\n\n<li><code>__tostring<\/code> \u7528\u4e8e\u8c03\u8bd5\u548c\u65e5\u5fd7 \u2014\u2014 \u5f3a\u70c8\u63a8\u8350\u4e3a\u5bf9\u8c61\u5b9e\u73b0<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 5.3 \u5bf9\u8c61\u7684\u4f7f\u7528\u89c4\u5219<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u72b6\u6001\uff08\u6570\u636e\uff09\u5b58\u5728\u5bf9\u8c61\u81ea\u8eab<\/li>\n\n\n\n<li>\u65b9\u6cd5\u5b58\u5728\u201c\u7c7b\u201d\u6216\u539f\u578b\u4e2d\uff08\u901a\u8fc7 <code>__index<\/code> \u7ee7\u627f\uff09<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>obj:method()<\/code> \u800c\u4e0d\u662f <code>obj.method(obj)<\/code><\/li>\n\n\n\n<li>\u907f\u514d\u5728\u5bf9\u8c61\u4e2d\u5b58\u50a8\u51fd\u6570\uff08\u9664\u975e\u662f\u95ed\u5305\u6216\u52a8\u6001\u751f\u6210\uff09\uff0c\u4ee5\u8282\u7701\u5185\u5b58<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 5.4 \u7c7b\u7684\u4f7f\u7528\u89c4\u5219<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u201c\u7c7b\u201d\u672c\u8eab\u4e5f\u662f\u4e00\u4e2a\u5bf9\u8c61\uff08\u539f\u578b\uff09<\/li>\n\n\n\n<li>\u6784\u9020\u51fd\u6570\u901a\u5e38\u53eb <code>new<\/code> \u6216 <code>create<\/code><\/li>\n\n\n\n<li>\u4f7f\u7528 <code>setmetatable(obj, { __index = self })<\/code> \u5b9e\u73b0\u7ee7\u627f<\/li>\n\n\n\n<li>\u65b9\u6cd5\u5b9a\u4e49\u5728\u201c\u7c7b\u201d\u4e0a\uff0c\u5bf9\u8c61\u901a\u8fc7 <code>__index<\/code> \u7ee7\u627f<\/li>\n\n\n\n<li>\u652f\u6301\u591a\u5c42\u7ee7\u627f\uff08<code>__index<\/code> \u94fe\uff09<\/li>\n\n\n\n<li>\u53ef\u9009\uff1a\u5b9e\u73b0 <code>__call<\/code> \u8ba9\u7c7b\u50cf\u51fd\u6570\u4e00\u6837\u88ab\u8c03\u7528<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 5.5 \u6027\u80fd\u6ce8\u610f\u4e8b\u9879<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9891\u7e41\u89e6\u53d1 <code>__index<\/code>\/<code>__newindex<\/code> \u6709\u6027\u80fd\u5f00\u9500 \u2192 \u907f\u514d\u5728\u70ed\u8def\u5f84\u4f7f\u7528<\/li>\n\n\n\n<li>\u5171\u4eab\u5143\u8868 \u2192 \u8282\u7701\u5185\u5b58<\/li>\n\n\n\n<li>\u65b9\u6cd5\u653e\u5728\u201c\u7c7b\u201d\u4e2d \u2192 \u907f\u514d\u6bcf\u4e2a\u5bf9\u8c61\u590d\u5236\u51fd\u6570<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 5.6 \u8c03\u8bd5\u5efa\u8bae<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>getmetatable(obj)<\/code> \u67e5\u770b\u5bf9\u8c61\u7684\u5143\u8868<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>debug.getmetatable<\/code>\uff08\u5982\u679c\u5143\u8868\u88ab\u4fdd\u62a4\uff09<\/li>\n\n\n\n<li>\u4e3a\u5bf9\u8c61\u5b9e\u73b0 <code>__tostring<\/code>\uff0c\u65b9\u4fbf print \u8c03\u8bd5<\/li>\n\n\n\n<li>\u907f\u514d\u8fc7\u5ea6\u4f7f\u7528\u5143\u8868 \u2014\u2014 \u4f1a\u8ba9\u4ee3\u7801\u884c\u4e3a\u201c\u4e0d\u76f4\u89c2\u201d<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udded \u516d\u3001\u5173\u7cfb\u56fe\u8c31\u603b\u7ed3<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>+----------------------+\n|       \u5143\u8868           |\n| (metatable)          |\n| - __index \u2192 \u7c7b      | \u2190 \u5b9a\u4e49\u201c\u7ee7\u627f\u201d\u884c\u4e3a\n| - __call             | \u2190 \u5b9a\u4e49\u201c\u53ef\u8c03\u7528\u201d\u884c\u4e3a\n| - __tostring         | \u2190 \u5b9a\u4e49\u201c\u6253\u5370\u201d\u884c\u4e3a\n+----------\u2191----------+\n           |\n           | setmetatable(obj, mt)\n           |\n+----------\u2193----------+\n|        \u8868            |\n| (table)              |\n| - x = 10             | \u2190 \u6570\u636e\uff08\u72b6\u6001\uff09\n| - y = 20             |\n| - move = function    | \u2190 \u65b9\u6cd5\uff08\u884c\u4e3a\uff09\u2192 \u901a\u5e38\u653e\u201c\u7c7b\u201d\u91cc\uff0c\u901a\u8fc7 __index \u7ee7\u627f\n+----------\u2191----------+\n           |\n           | \u662f\n           |\n+----------\u2193----------+\n|       \u5bf9\u8c61           | \u2190 \u5bf9\u8c61 = \u8868 + \u5143\u8868\n+----------\u2191----------+\n           |\n           | \u4ece...\u521b\u5efa\n           |\n+----------\u2193----------+\n|        \u7c7b            | \u2190 \u7c7b = \u539f\u578b\u8868 + \u5143\u8868\uff08\u901a\u5e38 __index \u6307\u5411\u81ea\u5df1\uff09\n| (\u5176\u5b9e\u662f\u539f\u578b\u5bf9\u8c61)     |\n+----------------------+<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2705 \u4e03\u3001\u4e00\u53e5\u8bdd\u603b\u7ed3<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u5728 Lua \u4e2d\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8868<\/strong> \u662f\u6570\u636e\u548c\u65b9\u6cd5\u7684\u5bb9\u5668\uff1b<\/li>\n\n\n\n<li><strong>\u5143\u8868<\/strong> \u662f\u8868\u7684\u884c\u4e3a\u63a7\u5236\u5668\uff1b<\/li>\n\n\n\n<li><strong>\u5bf9\u8c61<\/strong> \u662f\u4e00\u4e2a\u8868\uff08\u5e26\u72b6\u6001\uff09 + \u53ef\u9009\u5143\u8868\uff08\u5e26\u884c\u4e3a\uff09\uff1b<\/li>\n\n\n\n<li><strong>\u7c7b<\/strong> \u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u8868\uff08\u539f\u578b\uff09 + \u5143\u8868\uff08\u7528 <code>__index<\/code> \u5b9e\u73b0\u7ee7\u627f\uff09\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u7ba1\u7406\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\ud83e\uddf1 \u4e00\u3001\u8868\uff08table\uff09\u2014\u2014 Lua \u7684\u552f\u4e00\u590d\u5408\u6570\u636e\u7ed3\u6784 \u2705 1.1 \u672c\u8d28 \u2705 1.2 \u57fa\u672c\u8bed\u6cd5 \u2705 1.3  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2330,"comment_status":"open","ping_status":"open","sticky":false,"template":"wp-custom-template-my","format":"standard","meta":{"footnotes":""},"categories":[695,154],"tags":[690,688,382],"class_list":["post-2329","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-lua","category-154","tag-lua","tag-688","tag-382"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/2329","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2329"}],"version-history":[{"count":8,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/2329\/revisions"}],"predecessor-version":[{"id":2382,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/2329\/revisions\/2382"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/media\/2330"}],"wp:attachment":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}