lua面向对象(类)和lua协同线程与协同函数、Lua文件I/O

简介: Lua的面向对象编程、协同线程与协同函数的概念和使用,以及Lua文件I/O操作的基本方法。
-- create a class
Animal={name = "no_name" , age=0 }
function Animal:bark(voice)
        print(self.name.."in"..voice.."jiao")
end

function Animal:new()
    a={}
    setmetatable(a,{__index=self})
    return a
end
animal = Animal:new()
animal2=Animal:new()
-- print(animal)
-- print(animal2)

animal.name = "Tom"
animal.age = 8
animal.type = "cat"

print(animal.name.."jn"..animal.age)

function animal2:skill()
    return "mouse"
end

print(animal2.name.."jn"..animal.age..animal2:skill())

function Animal:new (obj)
        local a=obj or {}
        setmetatable(a,{__index=self})
        return a
end

Cat =Animal:new({type="bosiCat"})
Cat.eye="blue eye"
tomcat =Cat:new()
tomcat.name="Tome"
print(tomcat.name.."is".."d"..tomcat.type)

运行结果:

Lua协同线程

lua中有一种特色的线程,称为coroutine,协同线程,简称协程。其可以在运行时暂停执行,然后转去执行其他线程,然后还可以返回再继续执行没有完毕的内容。即可以“走走停停,停停走走”

在lua中表示独立的执行线程。任意时刻只会有一个协程执行,而不会出现多个协程同时执行的情况。

-- create thread
crt=coroutine.create(
  function(a,b)
     print(a,b,a+b)
     -- obtain running thread
     tr=coroutine.running();
     print(tr)
     -- check type
     print(type(tr))
     -- check crt  status
     print(coroutine.status(crt))
     -- coroutine pending
     coroutine.yield()
     print("back")

  end
)

-- start coroutine  a=3,b=5
coroutine.resume(crt,3,5)
-- check crt type
print("main-"..type(crt))
-- check crt  status
print("main-"..coroutine.status(crt))
-- cancel panding  math is not imporitance
coroutine.resume(crt,3,5)
-- check crt  status
print("main-"..coroutine.status(crt))

运行结果:

有返回值的协同线程

--create
crt=coroutine.create(
  function(a,b)
     print(a,b)
     c=a*b
     print(c)
     coroutine.yield(c,a/b)
     return a+b,a-b
    -- print("back")
  end
)

result,result1,result2 =  coroutine.resume(crt,4,7)
print(result,reslut1,result2)

协同函数:

cf= coroutine.wrap(
  function(a,b)
  print(a,b)
  return a+b,a*b
  end
)

success,result1,result2=cf(3,4)

结果:

Lua的IO

file= io.open("t1.lua","r")
io.input(file)
line=io.read()
print("hi")
while line ~= nil  do
   print(line)
   line=io.read("*1")
end
io.close(file)

File操作

file= io.open("t1.lua","a")
file:write("\nlevel=p7")
file:close()

结果:

目录
相关文章
|
5月前
|
存储 前端开发 C++
C++ 多线程之带返回值的线程处理函数
这篇文章介绍了在C++中使用`async`函数、`packaged_task`和`promise`三种方法来创建带返回值的线程处理函数。
180 6
|
5月前
|
C++
C++ 多线程之线程管理函数
这篇文章介绍了C++中多线程编程的几个关键函数,包括获取线程ID的`get_id()`,延时函数`sleep_for()`,线程让步函数`yield()`,以及阻塞线程直到指定时间的`sleep_until()`。
70 0
|
5月前
|
数据可视化 开发者 索引
详解Wireshark LUA插件函数:function p_myproto.dissector(buffer, pinfo, tree)
在 Wireshark 中,LUA 插件通过 `function p_myproto.dissector(buffer, pinfo, tree)` 扩展协议解析能力,解析自定义应用层协议。参数 `buffer` 是 `PacketBuffer` 类型,表示原始数据包内容;`pinfo` 是 `ProtoInfo` 类型,包含数据包元信息(如 IP 地址、协议类型等);`tree` 是
213 1
|
7月前
处理串口线程数据的函数
【8月更文挑战第4天】处理串口线程数据的函数。
46 4
|
8月前
|
Java Linux
Java演进问题之1:1线程模型对于I/O密集型任务如何解决
Java演进问题之1:1线程模型对于I/O密集型任务如何解决
|
7月前
|
Dart 编译器 API
Dart ffi 使用问题之在C++线程中无法直接调用Dart函数的问题如何解决
Dart ffi 使用问题之在C++线程中无法直接调用Dart函数的问题如何解决
|
9月前
|
程序员
老程序员分享:lua类实现
老程序员分享:lua类实现
44 2
|
9月前
|
XML 编译器 C++
一篇文章讲明白lua文件是什么?
一篇文章讲明白lua文件是什么?
152 1
|
9月前
|
C++
2dx lua自定义类
2dx lua自定义类
|
22天前
|
缓存 NoSQL 搜索推荐
【📕分布式锁通关指南 03】通过Lua脚本保证redis操作的原子性
本文介绍了如何通过Lua脚本在Redis中实现分布式锁的原子性操作,避免并发问题。首先讲解了Lua脚本的基本概念及其在Redis中的使用方法,包括通过`eval`指令执行Lua脚本和通过`script load`指令缓存脚本。接着详细展示了如何用Lua脚本实现加锁、解锁及可重入锁的功能,确保同一线程可以多次获取锁而不发生死锁。最后,通过代码示例演示了如何在实际业务中调用这些Lua脚本,确保锁操作的原子性和安全性。
52 6
【📕分布式锁通关指南 03】通过Lua脚本保证redis操作的原子性

热门文章

最新文章