{"id":2529,"date":"2024-09-11T14:06:55","date_gmt":"2024-09-11T06:06:55","guid":{"rendered":"https:\/\/courtship.top\/?p=2529"},"modified":"2024-09-11T14:06:55","modified_gmt":"2024-09-11T06:06:55","slug":"lua-%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/courtship.top\/index.php\/2024\/09\/11\/lua-%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1\/","title":{"rendered":"Lua \u9762\u5411\u5bf9\u8c61"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u7c7b\u8bbe\u8ba1<\/h2>\n\n\n\n<p>Lua\u5e76\u4e0d\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u8bed\u8a00\uff0cLua\u4e2d\u7684\u9762\u5411\u5bf9\u8c61\u90fd\u53ea\u662f\u5bf9\u771f\u6b63\u7684\u9762\u5411\u5bf9\u8c61\u7684\u4e00\u79cd\u6a21\u62df\uff0cLua\u4e4b\u6240\u4ee5\u80fd\u591f\u5b9e\u73b0\u8fd9\u79cd\u6a21\u62df\uff0c\u6e90\u4e8eLua\u4e2d\u7684\u4e09\u5251\u5ba2\uff1atable\uff0cmetatable\uff0c__index\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>local Account = { balance = 1000 }\n\nfunction Account.Withdraw(self, money)\n\n   self.balance = self.balance - money\n\nend\n\nlocal aa = Account\naa.Withdraw(aa, 100)\nprint(aa.balance)\nprint(Account.balance)\n\naa:Withdraw(100)\nprint(aa.balance)\nprint(Account.balance)\n\n\u8f93\u51fa\u7ed3\u679c\uff1a\n900\n900\n800\n800<\/code><\/pre>\n\n\n\n<p>\u8fd9\u5e76\u4e0d\u662f\u771f\u6b63\u7684\u5bf9\u8c61\uff0c\u5982\u6b64\u521b\u5efa\u4f1a\u5f62\u6210\u95ed\u5305\uff0c\u540c\u65f6\u4fee\u6539\u4e00\u4e2a\u5305\u91cc\u7684\u6570\u636e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class = { x=0, y=0 }\nClass.__index = Class\n\nfunction Class:new(x,y)\n    local self = { }  \n    setmetatable(self, Class)  \n    self.x = x   \n    self.y = y\n    return self \n\nend\n\nfunction Class:print()\n    print(self.x, self.y)\nend\n\nfunction Class:plus()\n    self.x = self.x + 1\n    self.y = self.y + 1\nend\n\na = Class:new(10, 20)\na:print()\nb = Class:new(11, 23)\n\nb:print()\nb:plus()\nb:print()\na:print()\n\n\u8f93\u51fa\u7ed3\u679c\uff1a\n10 20\n11 23\n12 24\n10 20<\/code><\/pre>\n\n\n\n<p>\u5c06\u81ea\u5df1\u7684\u5143\u8868\u6307\u5411\u81ea\u5df1\uff0c\u5e76\u8bbe\u7f6e\u3002\u5982\u6b64\u5c31\u53ef\u4ee5\u901a\u8fc7\u5143\u8868\uff0c\u5373\u4f7f\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u5185\u90e8\u53d8\u91cf\u5728\u81ea\u5df1\u7684\u5143\u8868\u5185\u7ba1\u7406\uff0c\u5f62\u6210\u4e86\u7c7b\u5bf9\u8c61\u7684\u529f\u80fd\uff0c\u5bf9\u8c61\u548c\u5bf9\u8c61\u4e4b\u95f4\u4e92\u76f8\u72ec\u7acb\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Lua \u7ee7\u627f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u57fa\u7c7b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>--\u7c7b\u7684\u5c5e\u6027\nlocal Vehicle = {\n    wheels = 4,\n    speed = 100,\n    color = \u201cb\u201d\n}\n\n--\u6709\u4e86\u8fd9\u4e00\u53e5\u624d\u80fd\u7b97\u662f\u4e00\u4e2a\u7c7b\uff0c\u56e0\u4e3a\u8fd9\u4e00\u53e5\u5b9e\u73b0\u4e86\u5b9e\u4f8b\u548c\u7c7b\u4e4b\u95f4\u7684\u5173\u8054\u6027\nVehicle.__index = Vehicle\n\n--\u6784\u9020\u51fd\u6570\nfunction Vehicle:New(wheels, speed, color)\n    local obj = {}\n    setmetatable(obj, self) --\u8fd9\u91cc\u548c\u4e0a\u9762\u7684__index\u662f\u9065\u76f8\u547c\u5e94\u7684\n    obj.wheels = wheels\n    obj.speed = speed\n    obj.color = color\n \n    return obj\nend\n\n--\u57fa\u7c7b\u7684\u65b9\u6cd5\nfunction Vehicle:getSpeed()\n    print(self.speed)\nend\n\nfunction Vehicle:setSpeed(x)\n    self.speed = x\nend\n\nfunction Vehicle:speedPluss()\n    self.speed = self.speed + 1\nend <\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u73b0\u7ee7\u627f<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>local Bicycle = { engine = 1 }\n\nsetmetatable(Bicycle, Vehicle) --\u8fd9\u91cc\u5c31\u5b9e\u73b0\u4e86Bicycle\u5bf9Vehicle\u7684\u7ee7\u627f\nBicycle.__index = Bicycle      --\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7c7b\uff0c\u4ee5\u4fbfBicycle\u7684\u6bcf\u4e2a\u5b9e\u4f8b\u90fd\u5230Bicycle\u8fdb\u884c\u67e5\u627e\n\nfunction Bicycle:New(engine)\n    local obj = {}\n    obj = Vehicle:New(2, 10, \"r\")\n    obj.engine = engine --\u9664\u4e86\u7ee7\u627f\u57fa\u7c7b\u6210\u5458\u5916\uff0c\u6dfb\u52a0\u81ea\u5df1\u6210\u5458\n \n    setmetatable(obj, self) --\u4f7f\u5f97Bicycle\u7684\u6bcf\u4e2a\u5b9e\u4f8b\u90fd\u4e0eBicycle\u6302\u94a9\n \n    return obj\nend\n\n--\u5bf9\u7236\u7c7b\u65b9\u6cd5\u7684\u91cd\u8f7d\n\nfunction Bicycle:setSpeed(s)\n    self.speed = s\n \nend\n\nfunction Bicycle:getSpeed()\n    print(self.speed)\nend \n\n--\u521b\u5efaBicycle\u7684\u4e24\u4e2a\u5bf9\u8c61\n\nob1 = Bicycle:New(2)\nob2 = Bicycle:New(3)\n\nob1:printAll()\nob2:printAll()\n\nob1:setSpeed(15)\nob2:speedPluss()\n\nob1:printAll()\nob2:printAll()\n\n\u8f93\u51fa\u7684\u7ed3\u679c\uff1a\n\n10\uff0c2\uff0cr\uff0c2\n10\uff0c2\uff0cr\uff0c3\n15\uff0c2\uff0cr\uff0c2\n11\uff0c2\uff0cr\uff0c3<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Lua \u591a\u6001<\/h2>\n\n\n\n<p>\u5728Lua\u4e2d\u6ca1\u6709<strong>\u7c7b\u578b<\/strong>\u7684\u6982\u5ff5\uff0c\u4e0d\u5b58\u5728\u50cfc++\u4e2d\u90a3\u6837\u7528\u57fa\u7c7b\u7684\u5f15\u7528\u6216\u6307\u9488\u6765\u6307\u5411\u6d3e\u751f\u7c7b\uff0c\u56e0\u6b64\uff0c\u5728Lua\u4e2d\u6a21\u62df\u7684\u591a\u6001\u53ea\u80fd\u8bf4\u662f\uff1a<strong>\u57fa\u7c7b\u751f\u6210\u7684\u5bf9\u8c61\u4f1a\u8c03\u7528\u57fa\u7c7b\u7684\u65b9\u6cd5\uff0c\u5982\u679c\u5b83\u7684\u5b50\u7c7b\u91cd\u5199\u4e86\u8be5\u65b9\u6cd5\uff0c\u90a3\u4e48\u5b50\u7c7b\u751f\u6210\u7684\u5bf9\u8c61\u5c31\u4f1a\u8c03\u7528\u5b50\u7c7b\u7684\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u800c\u4e0d\u4f1a\u53bb\u8c03\u7528\u57fa\u7c7b\u4e2d\u7684\u65b9\u6cd5\u3002<\/strong><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7c7b\u8bbe\u8ba1 Lua\u5e76\u4e0d\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u8bed\u8a00\uff0cLua\u4e2d\u7684\u9762\u5411\u5bf9\u8c61\u90fd\u53ea\u662f\u5bf9\u771f\u6b63\u7684\u9762\u5411\u5bf9\u8c61\u7684\u4e00\u79cd\u6a21\u62df\uff0cLua\u4e4b\u6240\u4ee5\u80fd\u591f\u5b9e\u73b0\u8fd9\u79cd\u6a21\u62df\uff0c\u6e90\u4e8eLua &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[24],"tags":[],"class_list":["post-2529","post","type-post","status-publish","format-standard","hentry","category-lua"],"_links":{"self":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/2529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/comments?post=2529"}],"version-history":[{"count":20,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/2529\/revisions"}],"predecessor-version":[{"id":2549,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/posts\/2529\/revisions\/2549"}],"wp:attachment":[{"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/media?parent=2529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/categories?post=2529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courtship.top\/index.php\/wp-json\/wp\/v2\/tags?post=2529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}