mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-05-05 14:28:31 +00:00
Merge pull request #2113 from qinhanlei/master
[lua/cn] synchronized with EN version. (revised edition)
This commit is contained in:
commit
8dffb33a8c
@ -92,7 +92,7 @@ until num == 0
|
|||||||
----------------------------------------------------
|
----------------------------------------------------
|
||||||
|
|
||||||
function fib(n)
|
function fib(n)
|
||||||
if n < 2 then return 1 end
|
if n < 2 then return n end
|
||||||
return fib(n - 2) + fib(n - 1)
|
return fib(n - 2) + fib(n - 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -130,8 +130,10 @@ f = function (x) return x * x end
|
|||||||
|
|
||||||
-- 这些也是等价的:
|
-- 这些也是等价的:
|
||||||
local function g(x) return math.sin(x) end
|
local function g(x) return math.sin(x) end
|
||||||
local g; g = function (x) return math.sin(x) end
|
local g; g = function (x) return math.sin(x) end
|
||||||
-- 'local g'使得g可以自引用。
|
-- 以上均因'local g',使得g可以自引用。
|
||||||
|
local g = function(x) return math.sin(x) end
|
||||||
|
-- 等价于 local function g(x)..., 但函数体中g不可自引用
|
||||||
|
|
||||||
-- 顺便提下,三角函数以弧度为单位。
|
-- 顺便提下,三角函数以弧度为单位。
|
||||||
|
|
||||||
@ -328,7 +330,7 @@ seymour:makeSound() -- 'woof woof woof' -- 4.
|
|||||||
|
|
||||||
-- 如果有必要,子类也可以有new(),与基类相似:
|
-- 如果有必要,子类也可以有new(),与基类相似:
|
||||||
function LoudDog:new()
|
function LoudDog:new()
|
||||||
newObj = {}
|
local newObj = {}
|
||||||
-- 初始化newObj
|
-- 初始化newObj
|
||||||
self.__index = self
|
self.__index = self
|
||||||
return setmetatable(newObj, self)
|
return setmetatable(newObj, self)
|
||||||
@ -340,7 +342,9 @@ end
|
|||||||
|
|
||||||
|
|
||||||
--[[ 我把这部分给注释了,这样脚本剩下的部分可以运行
|
--[[ 我把这部分给注释了,这样脚本剩下的部分可以运行
|
||||||
|
```
|
||||||
|
|
||||||
|
```lua
|
||||||
-- 假设文件mod.lua的内容类似这样:
|
-- 假设文件mod.lua的内容类似这样:
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
@ -411,4 +415,9 @@ lua-users.org上的[Lua简明参考](http://lua-users.org/files/wiki_insecure/us
|
|||||||
* <a href="http://lua-users.org/wiki/IoLibraryTutorial">io library</a>
|
* <a href="http://lua-users.org/wiki/IoLibraryTutorial">io library</a>
|
||||||
* <a href="http://lua-users.org/wiki/OsLibraryTutorial">os library</a>
|
* <a href="http://lua-users.org/wiki/OsLibraryTutorial">os library</a>
|
||||||
|
|
||||||
|
顺便说一下,整个文件是可运行的Lua;
|
||||||
|
保存为 learn-cn.lua 用命令 `lua learn.lua` 启动吧!
|
||||||
|
|
||||||
|
本文首次撰写于 [tylerneylon.com](http://tylerneylon.com) 同时也有 [github gist](https://gist.github.com/tylerneylon/5853042) 版.
|
||||||
|
|
||||||
使用Lua,欢乐常在!
|
使用Lua,欢乐常在!
|
||||||
|
Loading…
Reference in New Issue
Block a user