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
原文地址:http://blog.csdn.net/tingting14054765/article/details/40613935