Lua代码解析-写给C和C++开发人员
lua语言作为一门轻量级脚本语言,能够非常好的被嵌入到应用程序,因此,在移动游戏开发中举足轻重
然后C/C++开发人员转lua并非非常习惯,我也是。。所以,一起努力学习lua吧
lua没有类的概念,有table的概念
<span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);">function create(name,id) </span>
<span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);"> local obj={name = name ,id = id}</span>
<span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);"> function obj:setName(name)</span>
<span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);"> self.name=name</span>
<span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);"> end</span>
<span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);"> function obj:setID(id)</span>
<span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);"> self.id=id</span>
<span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);"> end</span>
end
lua能够用继承的概念
--基类
function createRobot(name,id)
local obj={name =name, id= id}
function obj:setName(name)
self.name=name
end
function obj:getName()
return self.name
end
function obj:setID(id)
self.id=id
end
function obj:getID()
return self.id
end
return obj
end
--派生类
function createFootballRobot(name,id,position)
local obj=createRobot(name,id)
obj.position="right"
return obj
end