{"id":178,"date":"2023-02-23T14:55:56","date_gmt":"2023-02-23T06:55:56","guid":{"rendered":"https:\/\/www.tom4kg.life\/?p=178"},"modified":"2023-02-23T15:03:21","modified_gmt":"2023-02-23T07:03:21","slug":"%e7%ae%80%e5%8d%95%e5%85%a5%e9%97%a8lua%e7%bc%96%e7%a8%8b%e8%af%ad%e8%a8%80","status":"publish","type":"post","link":"https:\/\/www.tom4kg.life\/?p=178","title":{"rendered":"\u7b80\u5355\u5165\u95e8Lua\u7f16\u7a0b\u8bed\u8a00"},"content":{"rendered":"\n<p>\u6700\u8fd1\u5728\u5b66\u4e60\u5199LuaSTG\u7684\u63d2\u4ef6\uff0c\u4f5c\u4e3a\u65b0\u624b\uff0c\u9996\u5148\u8981\u5b66\u4e60\u7684\u5c31\u662f\u5b83\u6240\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00lua\uff0c\u8fd9\u4e2a\u8bed\u8a00\u4e3b\u8981\u5e94\u7528\u5728nginx\u7684\u5f00\u53d1\u4e2d\u3002\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb0\u5f55\u5b66\u4e60\u8fc7\u7a0b\u4e2d\u7684\u4e00\u4e9b\u7ecf\u9a8c\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6709\u5e2e\u52a9\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Install \u5b89\u88c5<\/strong><\/h2>\n\n\n\n<p>\u5b66\u4e60\u7684\u7b2c\u4e00\u6b65\uff0c\u80af\u5b9a\u662f\u5b89\u88c5\u5bf9\u5e94\u7684\u5f00\u53d1\u73af\u5883\u3002Lua\u7684\u5f00\u53d1\u73af\u5883\uff0c\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u6709\u4e0d\u540c\u7684\u5b89\u88c5\u65b9\u6cd5\uff0c\u6bd4\u8f83\u7b80\u5355\u7684\u662f\u5728MAC\u4e0a\u4e00\u6761\u547d\u4ee4\u5c31\u641e\u5b9a\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>brew install lua<\/code><\/pre>\n\n\n\n<p>Windows\u5c31\u4e0b\u8f7d\u5bf9\u5e94\u7684\u4e8c\u8fdb\u5236\u5b89\u88c5\u5305\u8fdb\u884c\u5b89\u88c5\u5c31\u597d\u4e86\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Hello World<\/strong><\/h2>\n\n\n\n<p>\u5b89\u88c5\u5b8c\u73af\u5883\u540e\uff0c\u6211\u4eec\u6765\u5199\u4e00\u4e2ahello world\u7684\u7a0b\u5e8f\uff0c\u6765\u6d4b\u8bd5\u4e00\u4e0b\u73af\u5883\u662f\u5426\u6b63\u5e38\u3002<\/p>\n\n\n\n<p>\u65b0\u5efa\u4e00\u4e2ahelloworld.lua\u7684\u6587\u4ef6:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(\"Hello World\")<\/code><\/pre>\n\n\n\n<p>\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165:<\/p>\n\n\n\n<p>lua helloworld.lua<\/p>\n\n\n\n<p>\u770b\u5230\u8f93\u51fa\u719f\u6089\u7684hello world\uff0c\u5c31\u8bc1\u660e\u4e00\u5207\u6b63\u5e38\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Comment\u6ce8\u91ca<\/strong><\/h2>\n\n\n\n<p>\u4e0b\u6765\u5c31\u662f\u8bed\u8a00\u7684\u57fa\u672c\u8bed\u6cd5\uff0c\u5148\u5b66\u4e60\u4e00\u4e0b\u6ce8\u91ca\u7684\u4f7f\u7528\u3002lua\u8bed\u8a00\u5f53\u4e2d\u91c7\u7528\u7684\u662f\u2013\u4f5c\u4e3a\u6ce8\u91ca\u6807\u793a\u7b26\uff0c\u5c31\u50cf\u8fd9\u6837:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>--- comment<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u53d8\u91cf\u58f0\u660e<\/strong><\/h2>\n\n\n\n<p>\u4f5c\u4e3a\u5f31\u7c7b\u578b\u8bed\u8a00\uff0c\u5728\u58f0\u660e\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u4e0d\u5fc5\u53bb\u6307\u5b9a\u5b83\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n\n\n\n<p>\u6765\u770b\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name = \"scq000\"\n\nio.write(\"Length of name\", #name, \"\\n\")\n\nage = 4\n\nio.write(\"My age is \", age, \"\\n\")<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528<code>io.write<\/code>\u51fd\u6570\u76f4\u63a5\u8f93\u51fa\u5bf9\u5e94\u7684\u53d8\u91cf\u540d\u7b49\uff0c\u5176\u4e2d<code>#<\/code>\u4f5c\u4e3a\u8fd0\u7b97\u7b26\u53ef\u4ee5\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u8981\u751f\u6210\u4e00\u4e2a\u957f\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u8fd9\u79cd\u5199\u6cd5:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>longString = &#91;&#91;\n  I am a very long long string that \n  dfsfdsfsdfs\n]]\nio.write(longString, \"\\n\")\n\nlongString = longString .. name\n-- \u8fde\u63a5\u64cd\u4f5c\nio.write(longString, \"\\n\")<\/code><\/pre>\n\n\n\n<p>\u6570\u636e\u7c7b\u578b\u7684\u8bdd\uff0clua\u8bed\u8a00\u5f53\u4e2d\u53ea\u652f\u6301\u6d6e\u70b9\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bigNum = 9234324342342 + 1\nio.write(\"Big Number\" , type(bigNum), \"\\n)\n\nfloatPrecision = 1.99999999999999 + 0.0000000000000005\nfloatPrecision = 1.99999999999999 + 0.00000000000000055<\/code><\/pre>\n\n\n\n<p>\u83b7\u53d6\u53d8\u91cf\u7c7b\u578b\u4fe1\u606f<\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528type\u51fd\u6570\u6765\u83b7\u53d6\u53d8\u91cf\u7684\u57fa\u672c\u7c7b\u578b:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>isWrite = true\nio.write(type(isWrite))<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Math Function<\/strong><\/h2>\n\n\n\n<p>\u548c\u5176\u4ed6\u8bed\u8a00\u7c7b\u4f3c\uff0c\u5728lua\u8bed\u8a00\u5185\u90e8\u5305\u542b\u4e86\u5f88\u591a\u5185\u7f6e\u7684\u6570\u5b66\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>io.write(\"5 + 3 = \", \"\\n\")\nio.write(\"5 * 3 = \")\n\n-- number\n-- floor ceil max min, sin, cos, sqrt\n\nmath.floor(134.33223)\nmath.random()\nmath.random(10)\nmath.random(5, 100)\n\nmath.randomseed(os.time())\n\nprint(string.format(\"Pi = %.10f\", math.pi))<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u903b\u8f91\u8fd0\u7b97\u7b26<\/strong><\/h2>\n\n\n\n<p>\u5173\u7cfb\u8fd0\u7b97\u7b26\u548c\u5176\u4ed6\u8bed\u8a00\u5927\u540c\u5c0f\u5f02\uff0c\u903b\u8f91\u8fd0\u7b97\u7b26\u91c7\u7528\u7684\u662f\u5355\u8bcd\u5f62\u5f0f and or not\uff0c\u7b80\u5355\u7684\u4f8b\u5b50\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- Relational Operators: &gt; &lt; &gt;= &lt;= == ~=\nage = 13\nif age &lt; 16 then\n  io.write(\"You can go to school\", \"\\n\")\n  local localVar = 10\nelseif (age &lt;= 16) and (age &lt; 18) then\n  io.write(\"You can drive\", \"\\n\")\nelse\n  io.write(\"You can vote\", \"\\n\")\nend\n\n-- Logical Operators: and or not\n\ntoString(not true)\ncanVote = age &gt; 18 ? true : false\n\ncanvote = age &gt; 18 and true or false<\/code><\/pre>\n\n\n\n<p>lua\u8bed\u8a00\u4e2d\u7684\u4ee3\u7801\u5206\u5757\u91c7\u7528end\u8fdb\u884c\u7ed3\u5c3e\uff0c\u548c\u5176\u5b83\u8bed\u8a00\u4e2d\u4f7f\u7528<code>{}<\/code>\u8fdb\u884c\u5206\u5757\u4e0d\u592a\u4e00\u6837\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u5b57\u7b26\u4e32\u64cd\u4f5c<\/strong><\/h2>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- get length\nstring.len(quote)\n-- or\n#quote\n\n-- replace string\nstring.gsub(quote, \"I\", \"me\")\n\n-- find and get index\nstring.find(quote, \"password\")\n\n-- upper \/lower\nstring.upper(quote)\nstring.lower(quote)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u5faa\u73af\uff0c\u5206\u652f\u63a7\u5236\u8bed\u53e5<\/strong><\/h2>\n\n\n\n<p>lua\u4e2d\u652f\u6301\u7684\u5faa\u73af\u8bed\u53e5\u7c7b\u578b\u6bd4\u8f83\u591a\uff1a<\/p>\n\n\n\n<ol><li>while\u5faa\u73af\uff1a<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>i = 1\n\nwhile (i &lt;= 10) do\n  io.write(i)\n  i = i + 1\n  \n  if i == 8 then break end\nend<\/code><\/pre>\n\n\n\n<p>2.until\u5faa\u73af\uff1a \u548c\u5176\u5b83\u8bed\u8a00\u4e2ddo\u2026until\u7c7b\u4f3c\uff0c\u8fd9\u91cc\u91c7\u7528\u7684\u662frepeat\u2026until\u7684\u8bed\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>repeat\n  io.write(\"enter your guess:\")\n  \n  guess = io.read()\n  \nuntil tonumber(guess) == 15<\/code><\/pre>\n\n\n\n<p>3.for\u5faa\u73af\uff1a \u5faa\u73af\u4f53\u7684\u5199\u6cd5\u4e0d\u9700\u8981\u7528\u62ec\u53f7\u8fdb\u884c\u5206\u9694\uff0c\u91c7\u7528for\u2026do\u7684\u8bed\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i = 1, 10, 1 do\n  io.write(i)\nend\n\nmonths = {\"Juanary\", \"February\", \"March\", \"April\"}\n\nfor key, value in pairs(months) do\n  io.write(value, \" \")\nend<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Table<\/strong><\/h2>\n\n\n\n<p>\u5728lua\u8bed\u8a00\u4e2d\uff0c\u652f\u6301\u4e00\u79cdtable\u7684\u62bd\u8c61\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u952e\u503c\u5bf9\u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aTable = {}\n\nfor i = 1, 10 do\n  aTable&#91;i] = i\nend\n\nio.write(\"First : \", aTable&#91;1])\n\nio.write(\"Number of Items: \", #aTable, \"\\n\")\n\ntable.insert(aTable, 1, 0)\n\nio.write(\"First : \", aTable&#91;1])\n\n-- remove\ntable.remove(aTable\uff0c 1)\n\n-- convert to string\nprint(table.concat(aTable, \", \"))<\/code><\/pre>\n\n\n\n<p>\u4e8c\u7ef4\u8868\u7ed3\u6784\u7684\u5b9a\u4e49\u4e0e\u4e0a\u9762\u7684\u4f8b\u5b50\u5dee\u4e0d\u591a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aMultiTable = {}\n\nfor i = 0, 9 do\n  aMultiTable&#91;i] = {}\n  for j = 0, 9 do\n    aMultiTable&#91;i]&#91;j] = tostring(i) .. tostring(j)\n  end\n  print()\nend\n\nio.write(\"Table&#91;0]&#91;0] : \", aMultiTable&#91;0]&#91;0], \"\\n\")<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>functions<\/strong><\/h2>\n\n\n\n<p>\u51fd\u6570\u662f\u8fdb\u884c\u8bed\u8a00\u62bd\u8c61\u5316\u5fc5\u5907\u7684\u5de5\u5177\uff0c\u6240\u4ee5\u5728lua\u8bed\u8a00\u4e2d\uff0c\u76f4\u63a5\u4f7f\u7528function\u6765\u5b9a\u4e49\u5bf9\u5e94\u7684\u51fd\u6570\u5c31\u53ef\u4ee5\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function getSum(num1, num2)\n  return num1 + num2\nend\n\nprint(string.format(\" 2 + 6= %d\", getSum(2,6)<\/code><\/pre>\n\n\n\n<p>\u4e0b\u9762\u8fd9\u91cc\u4f8b\u5b50\u6f14\u793a\u4e86\u5982\u4f55\u4e00\u4e2a\u5b9e\u73b0\u5206\u5272\u5b57\u7b26\u4e32\u529f\u80fd\u7684\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function splitStr(theString)\n  stringTable = {}\n  \n  local i = 1\n  \n  for str in string.gmatch(theString, \"&#91;^%s]+\") do\n    stringTable&#91;i] = str\n    i + i + 1\n  end\n \n  return stringTable, i\nend\n\nsplitStrTable, numOfStr = splitStr(\"The Turtle\")\n\nfor j = 1, numOfStr do\n  print(string.format(\"%d : %s\", j splitStrTable&#91;j]))\nend<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u5728\u51fd\u6570\u5185\u90e8\u58f0\u660e\u5c40\u90e8\u53d8\u91cf\u8981\u4f7f\u7528local\u5173\u952e\u5b57\uff0c\u4ee5\u786e\u5b9a\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u3002<\/p>\n\n\n\n<p>function\u7684\u53c2\u6570\u652f\u6301\u4e0d\u5b9a\u957f\u53c2\u6570\uff0c\u4f7f\u7528<code>...<\/code>\u8bed\u6cd5\u5c31\u53ef\u4ee5\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function getSumMore(...)\n  local sum = 0\n  \n  for k, v in pairs{...} do\n    sum = sum + v\n  end\n  return sum\nend\n\nio.write(\"Sum \", getSomeMore(1,2,3,4,5,6), \"\\n\")<\/code><\/pre>\n\n\n\n<p>\u95ed\u5305\u8fd9\u4e2a\u7ed3\u6784\u5728\u51fd\u6570\u5f0f\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7b97\u662f\u6bd4\u8f83\u5e38\u89c1\u7684\uff0c\u5185\u90e8\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u5b83\u4e0a\u5c42\u6240\u5b9a\u4e49\u7684\u5c40\u90e8\u53d8\u91cf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>doubleIt = function(x) return x * 2 end\n\nprint(doubleIt(4))\n\n--- \u8f93\u51fa 8\n\nfunction outerFunc()\n  local i = 0\n  \n  return function()\n    i = i + 1\n    return i\n  end\nend\n\ngetI = outerFunc()\n\nprint(getI())\n\n-- \u8f93\u51fa 1<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>coroutine \u534f\u7a0b<\/strong><\/h2>\n\n\n\n<p>\u534f\u7a0b\u662f\u8ddf\u7ebf\u7a0b\u7c7b\u4f3c\u7684\u6982\u5ff5\uff0c\u4f46\u5728\u7c92\u5ea6\u4e0a\u6bd4\u7ebf\u7a0b\u8981\u5c0f\uff0c\u4e3b\u8981\u662f\u5728\u5176\u81ea\u8eab\u4e0a\u4e0b\u6587\u4e2d\u8fdb\u884c\u5207\u6362\u3002\u800c\u4e14\u5404\u4e2a\u534f\u7a0b\u9700\u8981\u8fdb\u884c\u534f\u4f5c\u624d\u80fd\u5f88\u597d\u5730\u5de5\u4f5c\u3002\u4e00\u4e2a\u7a0b\u5e8f\u5728\u540c\u4e00\u65f6\u95f4\u53ea\u80fd\u8fd0\u884c\u4e00\u4e2a\u534f\u7a0b\uff0c\u800c\u4e14\u534f\u7a0b\u8981\u663e\u793a\u6302\u8d77\u624d\u80fd\u4e0d\u5f3a\u5360\u8d44\u6e90\u3002<\/p>\n\n\n\n<p>\u4e3b\u8981\u7684\u65b9\u6cd5\u6709create, resume, yield, running, status\u7b49\uff0c\u5206\u522b\u7528\u6765\u8fdb\u884c\u521b\u5efa\u3001\u6062\u590d\u3001\u4f20\u53c2\u3001\u8fd4\u56de\u6b63\u5728\u8fd0\u884c\u7684\u534f\u7a0b\u548c\u8fd4\u56de\u534f\u7a0b\u72b6\u6001\u7b49\u64cd\u4f5c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>co = coroutine.create(function()\n\tfor i = 1, 10, 1 do\n      print(i)\n      print(coroutine.status(co))\n      if i == 5 then coroutine.yield() end\n      end\n    end\n)\n\nprint(coroutine.status(co))\n\ncoroutine.resume(co)\n\nprint(coroutine.status(co))<\/code><\/pre>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt; print(coroutine.status(co))\nsuspended\n&gt;\n&gt; coroutine.resume(co)\n1\nrunning\n2\nrunning\n3\nrunning\n4\nrunning\n5\nrunning\ntrue\n&gt;\n&gt; print(coroutine.status(co))\nsuspended<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>file\u6587\u4ef6\u64cd\u4f5c<\/strong><\/h2>\n\n\n\n<p>\u6587\u4ef6\u64cd\u4f5c\u4e3b\u8981\u5229\u7528io\u8fd9\u4e2a\u63a5\u53e3\u4e2d\u3002\u5e38\u7528\u7684\u8bed\u6cd5\u548cc\u8bed\u8a00\u7b49\u90fd\u5f88\u7c7b\u4f3c\uff0c\u8fd0\u884c\u4e00\u4e2a\u5b9e\u4f8b\u65b9\u6cd5\u7528\u91c7\u7528<code>:<\/code>\u8fd9\u79cd\u8bed\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- r\n-- w\n-- a: Append\n-- r+\uff1a read &amp; write existing file\n-- w+: overwrite read or create a file\n-- a+: append read or create file\n\nfile = io.open(\"test.lua\", \"w+\")\n\nfile:write(\"Random String of text\\n\")\nfile:write(\"Some more text\\n\")\n\nfile:seek(\"set\", 0)\n\nprint(file:read(\"*a\"))\n\nfile:close()\n\nfile = io.open(\"test.lua\", \"a+\")\n\nfile:write(\"Even more text\\n\")\nfile:seek(\"set\", 0)\n\nprint(file:read(\"*a))\n\nfile:close()<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Modules<\/strong><\/h2>\n\n\n\n<p>\u5728\u8fdb\u884c\u5927\u5de5\u7a0b\u5316\u5f00\u53d1\uff0c\u6a21\u5757\u5316\u662f\u5fc5\u987b\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u76f4\u63a5\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u58f0\u660e\u4e00\u4e2a\u6a21\u5757\uff0c\u5e76\u5c06\u8fd9\u4e2a\u6a21\u5757\u5bfc\u51fa\uff1a<\/p>\n\n\n\n<p>\u65b0\u5efa\u4e00\u4e2acovert.lua\u7684\u6587\u4ef6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- covert.lua\n\nlocal convert = {}\n\nfunction convert.ftToCm(feet)\n  return feet + 30.48\nend\n\nreturn convert<\/code><\/pre>\n\n\n\n<p>\u5728\u53e6\u4e00\u4e2a\u5165\u53e3\u6587\u4ef6index.lua\u4e2d\uff0c\u4f7f\u7528<code>require<\/code>\u8bed\u6cd5\u8fdb\u884c\u5f15\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>convertModule = require(\"convert\")\n\nprint(string.format(\"%.3f cm\", convertModule.ftToCm(12)))<\/code><\/pre>\n\n\n\n<p>\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884clua index.lua\u540e\u7ed3\u679c\u663e\u793a\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>42.480 cm<\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>MetaTable<\/strong><\/h2>\n\n\n\n<p>lua\u4e2d\u6709\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u662f\u5143\u8868\uff0c\u548c\u54c8\u5e0c\u8868\u7c7b\u4f3c\uff0c\u901a\u5e38\u53ef\u4ee5\u628a\u5b83\u770b\u505a\u662f\u4e00\u4e2a\u5907\u7528\u67e5\u627e\u8868\u3002\u5728Lua\u4e2d\u5728\u4e00\u4e2a\u8868\u4e2d\u67e5\u627e\u5143\u7d20\uff0c\u5982\u679c\u6ca1\u6709\u627e\u5230\uff0c\u4f1a\u53bb\u5176\u5173\u8054\u7684\u5143\u8868\u4e2d\u7ee7\u7eed\u8fdb\u884c\u67e5\u627e\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528setmetatable\u65b9\u6cd5\u53ef\u4ee5\u7ed9\u4e00\u4e2a\u8868\u5bf9\u8c61\u65b0\u589e\u4e00\u4e2a\u5143\u8868\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u81ea\u5df1\u53bb\u5b9a\u4e49\u5143\u8868\u7684\u64cd\u4f5c\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aTable = {}\n\nfor x = 1, 10 do\n  aTable&#91;x] = x\nend\n\n--     _sub _mul _div _mod _concat\nmt = {\n __add = function(table1, table2)\n    sumTable = {}\n    \n    for y = 1, #table1 do\n      if(table1&#91;y] ~= nil) and (table2&#91;y] ~= nil) then\n        sumTable&#91;y] = table1&#91;y] + table2&#91;y]\n      else\n        sumTable&#91;y] = 0\n      end\n    end\n    \n    return sumTable\n end,\n  \n  __eq = function(table1 table2)\n    return table1.value = table2.value\n  end\n}\n\nsetmetatable(aTable, mt)\n\nprint(aTable == aTable)\n\n--- print true<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Class OOP \u9762\u5411\u5bf9\u8c61<\/strong><\/h2>\n\n\n\n<p>lua\u4e5f\u652f\u6301\u9762\u5411\u5bf9\u8c61\u7684\u8bed\u8a00\u673a\u5236\uff0c\u5728\u65b0\u5efa\u4e00\u4e2a\u5bf9\u8c61\u7684\u65f6\u5019\uff0c\u4f1a\u8c03\u7528\u5176<code>new<\/code>\u65b9\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Animal = {height = 0, weight = 0, name = \"No Name\", sound = \"No Sound\"}\n\nfunction Animal:new (height, weight, name, sound)\n  setmetatable({}, Animal)\n  \n  self.height = height\n  self.weight = weight\n  self.name = name\n  self.sound = sound\n \n  return self\nend\n\nfunction Animal:toString()\n  animalStr = string.format(\"%s weighs %.1f lbs, is %.1f in tall and says %s\", self.name, self.height, self.weight, self.sound)\n  return animalStr\nend\n\nspot = Animal:new(10, 15, \"Spot\", \"woof\")\n\nprint(spot:toString())\n\n--- print Spot weighs 10.0 lbs, is 15.0 in tall and says woof<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p>\u5927\u591a\u6570\u7684lua\u8bed\u8a00\u90fd\u548c\u5176\u5b83\u7684\u73b0\u4ee3\u7f16\u7a0b\u8bed\u8a00\u5927\u540c\u5c0f\u5f02\uff0c\u5982\u679c\u4f60\u5df2\u7ecf\u6709\u4e86\u5176\u5b83\u7684\u8bed\u8a00\u7684\u57fa\u7840\uff0c\u5728\u5b66\u4e60\u7684\u65f6\u5019\u591a\u5bf9\u6bd4\uff0c\u591a\u603b\u7ed3\uff0c\u53ef\u4ee5\u52a0\u901f\u4f60\u638c\u63e1\u7684\u901f\u5ea6\u3002\u719f\u6089\u4e86\u5927\u81f4\u7684\u8bed\u8a00\u8bed\u6cd5\u4e4b\u540e\u5c31\u53ef\u4ee5\u6295\u5165\u5b9e\u8df5\u4e2d\uff0c\u591a\u5199\u4e00\u4e9bdemo\uff0c\u5c31\u80fd\u719f\u80fd\u751f\u5de7\u4e86\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u53c2\u8003\u8d44\u6599<\/strong><\/h2>\n\n\n\n<p><a href=\"https:\/\/link.zhihu.com\/?target=http%3A\/\/www.lua.org\/docs.html\" target=\"_blank\" rel=\"noreferrer noopener\">http:\/\/www.lua.org\/docs.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728\u5b66\u4e60\u5199LuaSTG\u7684\u63d2\u4ef6\uff0c\u4f5c\u4e3a\u65b0\u624b\uff0c\u9996\u5148\u8981\u5b66\u4e60\u7684\u5c31\u662f\u5b83\u6240\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00lua\uff0c\u8fd9\u4e2a\u8bed\u8a00\u4e3b\u8981\u5e94\u7528\u5728ngin [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"h5ap_radio_sources":[]},"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/www.tom4kg.life\/index.php?rest_route=\/wp\/v2\/posts\/178"}],"collection":[{"href":"https:\/\/www.tom4kg.life\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tom4kg.life\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tom4kg.life\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tom4kg.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=178"}],"version-history":[{"count":1,"href":"https:\/\/www.tom4kg.life\/index.php?rest_route=\/wp\/v2\/posts\/178\/revisions"}],"predecessor-version":[{"id":179,"href":"https:\/\/www.tom4kg.life\/index.php?rest_route=\/wp\/v2\/posts\/178\/revisions\/179"}],"wp:attachment":[{"href":"https:\/\/www.tom4kg.life\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tom4kg.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tom4kg.life\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}