and use it like this:
require "hello.mytest"hello.mytest.test2()但是这样写更好These problems can be avoided by not using the module
function but instead simply defining modules in the following simple way:
and importing modules this way:
local MT = require "hello.mytest"MT.test2()带有构造函数的类A module containing a class with constructor (in the object-oriented sense) can be packaged in a number of ways in a module. Here is one reasonably good approach.[*2]
-- file: finance/BankAccount.lualocal M = {}; M.__index = Mlocal function construct() local self = setmetatable({balance = 0}, M) return selfendsetmetatable(M, {__call = construct})function M:add(value) self.balance = self.balance + 1 endreturn MA module defined in this way typically only contains a single class (or at least a single public one), which is the module itself.
It can be used like this:
local BankAccount = require "finance.BankAccount"local account = BankAccount()or even like this:
local new = requirelocal account = new "finance.BankAccount" ()四、注释在--后面加上空格使用doxygen或者javadoc的注释方式:
-- taken from cgilua/src/cgilua/session.lua--------------------------------------- Deletes a session.-- @param id Session identification.-------------------------------------function delete (id) assert (check_id (id)) remove (filename (id))end在某个函数终止的后面加上注释Because "end
" is a terminator for many different constructs, it can help the reader (especially in a large block) if a comment is used to clarify which construct is being terminated: [*3]
However, if the variable tested can ever contain false
as well, then you will need to be explicit if the two conditions must be differentiated: line == nil
v.s. line == false
.
and
and or
may be used for terser code:
Clone a small table t
(warning: this has a system dependent limit on table size; it was just over 2000 on one system):
Determine if a table t
is empty (including non-integer keys, which #t
ignores):
t[#t+1] = 1更快速
To append to an array, it can be terser and more efficient to do t[#t+1] = 1
rather than table.insert(t, 1)
.六、lua的面向对象http://lua-users.org/wiki/ObjectOrientationTutorial闲来无事,就把以前翻译的规范发上来了。转载请注明出处:http://blog.csdn.net/xizero00新闻热点
疑难解答