Lua官方接口:LuatOS 文档
其他资料—》资料存档—》lua5.3参考手册
接口如何使用?
比如math.abs (x)
返回 x 的绝对值。(integer/float)
n = math.abs( -1 )
print(n) -- 1
--string.find (s, pattern [, init [, plain]]) s和pattern两个参数是必填的,后面两个参数是可选的,如果找到一个匹配,
--find 会返回 s 中关于它起始及终点位置的索引; 否则,返回 nil。
--第三个可选数字参数 init 指明从哪里开始搜索; 默认值为 1 ,同时可以是负值。
--第四个可选参数 plain 为 true 时, 关闭模式匹配机制。 此时函数仅做直接的 “查找子串”的操作, 而 pattern 中没有字符被看作魔法字符。
--注意,如果给定了 plain ,就必须写上 init 。
s1 = string.find("abcd","bc")
print(s1) -- bc在字符串"abcd"索引的位置 2 3
s2 = string.find("abcd","bd")
print(s2) -- nil
--多文件调用 需要用到一个函数require
--在vscode里面新建test.lua文件,在同级目录下放lua.exe;这个lua.exe可以直接调用test.lua文件
--require作用就是运行指定文件
--在同级目录下新建hello.lua文件(print("hello world"))在test.lua文件下 require("hello") hello是文件名,便可以输出hello.lua文件内容
--require用法
--[[
1.运行指定文件
2.末尾不带拓展名:require("hello")
3.目录层级用"."分割:
比如:path文件夹和test.lua文件和hello.lua文件是同级
path
p.lua
test.lua
hello.lua
在test.lua文件里要运行p.lua文件 require("path.p")就行
4.只会运行一次
在hello.lua文件
print("hello world")
return "done"
在test.lua文件里
local c = require("hello")
print(c) -- hello world ;done
在hello.lua文件
_G.count = _G.count + 1
print("hello world")
return "done"
在test.lua文件里
_G.count = 1
require("hello")
require("hello")
require("hello")
require("hello")
local c = require("hello")
print(c)
print(_G.cont) -- 2只加一次
5.从package.path路径中查找
把路径添加到package.path中 package.path = package.path..";./path/?.lua" 在require("p") 就行
]]
print(package.path) -- ./lua/?.lua 这就是为什么require不需要添加文件后缀
--多次调用
--一般来说require函数只是来调用我们外部库,所以不需要多次调用
--多次调用可以使用do while等函数实现,看手册
--平常情况下如何制作我们常用的库
--比如说table.xxxxx函数如何实现
--[[
在hello.lua文件
local table = {}
--如何添加函数
function table.say() 让say函数直接位于table下
print("hello world")
end
return table
在test.lua文件里
local table = require("hello")
table.say()
]]
591

被折叠的 条评论
为什么被折叠?



