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()

结果:

目录
相关文章
|
2月前
|
存储 安全 Java
【Java集合类面试二十五】、有哪些线程安全的List?
线程安全的List包括Vector、Collections.SynchronizedList和CopyOnWriteArrayList,其中CopyOnWriteArrayList通过复制底层数组实现写操作,提供了最优的线程安全性能。
|
4天前
|
数据可视化 开发者 索引
详解Wireshark LUA插件函数:function p_myproto.dissector(buffer, pinfo, tree)
在 Wireshark 中,LUA 插件通过 `function p_myproto.dissector(buffer, pinfo, tree)` 扩展协议解析能力,解析自定义应用层协议。参数 `buffer` 是 `PacketBuffer` 类型,表示原始数据包内容;`pinfo` 是 `ProtoInfo` 类型,包含数据包元信息(如 IP 地址、协议类型等);`tree` 是
8 1
|
21小时前
|
Java C++
【多线程】JUC的常见类,Callable接口,ReentranLock,Semaphore,CountDownLatch
【多线程】JUC的常见类,Callable接口,ReentranLock,Semaphore,CountDownLatch
9 0
|
2月前
|
安全 Java 调度
|
2月前
|
安全 Java 程序员
线程安全与 Vector 类的分析
【8月更文挑战第22天】
25 4
|
2月前
|
安全 Java
【Java集合类面试十三】、HashMap如何实现线程安全?
实现HashMap线程安全的方法包括使用Hashtable类、ConcurrentHashMap,或通过Collections工具类将HashMap包装成线程安全的Map。
|
2月前
|
Java
【Java集合类面试十二】、HashMap为什么线程不安全?
HashMap在并发环境下执行put操作可能导致循环链表的形成,进而引起死循环,因而它是线程不安全的。
|
2月前
|
安全 Java API
Java多线程编程:使用Atomic类实现原子操作
在Java多线程环境中,共享资源的并发访问可能导致数据不一致。传统的同步机制如`synchronized`关键字或显式锁虽能保障数据一致性,但在高并发场景下可能导致线程阻塞和性能下降。为此,Java提供了`java.util.concurrent.atomic`包下的原子类,利用底层硬件的原子操作确保变量更新的原子性,实现无锁线程安全。
16 0
【多线程面试题 二】、 说说Thread类的常用方法
Thread类的常用方法包括构造方法(如Thread()、Thread(Runnable target)等)、静态方法(如currentThread()、sleep(long millis)、yield()等)和实例方法(如getId()、getName()、interrupt()、join()等),用于线程的创建、控制和管理。
|
2月前
|
安全 Java
【Java集合类面试五】、 如何得到一个线程安全的Map?
如何得到一个线程安全的Map的方法包括:使用Collections工具类将Map包装为线程安全,使用java.util.concurrent包下的ConcurrentHashMap,以及不推荐使用性能较差的Hashtable。