标签:style blog strong 数据 io 2014
a = { } b = { x = 1, ["hello, "] = "world!" } a.astring = "ni, hao!" a[1] = 100 a["a table"] = b function foo() end function bar() end a[foo] = bar --分别穷举表a和b for k, v in pairs(a) do print(k, "=>", v) end print("----------------------------") for k, v in pairs(b) do print(k, "=>", v) end
--输出结果 1 => 100 a table => table: 003FB3A0 function: 003FCBB0 => function: 003FCBD0 astring => ni, hao! ---------------------------- hello, => world! x => 1定义表(Table)的方式:a = {}, b = {…}
function CreateStudent(ID,Name) local Obj={id=ID,name=Name}; function Obj:GetID() return self.id; end function Obj.GetName(self) return self.name; end function Obj:SetID(ID) self.id=ID; end Obj.SetName=function(self,Name) self.name=Name end return Obj; end s1=CreateStudent(1,"andy"); print("s1‘id=",s1:GetID(),"s1‘name=",s1.GetName(s1)) s1:SetID(2); s1.SetName(s1,"lili"); print("s1‘id=",s1:GetID(),"s1‘name=",s1:GetName()) --输出结果 --s1‘id= 1 s1‘name= andy --s1‘id= 2 s1‘name= lili
对象工厂模式:如上面代码的create函数
成员方法的调用:obj:method(a1, a2, …) <==>obj.method(obj, a1, a2, ...)
function CreateStudent(ID,Name) local Obj={id=ID,name=Name}; function Obj:GetID() return self.id; end function Obj.GetName(self) return self.name; end function Obj:SetID(ID) self.id=ID; end Obj.SetName=function(self,Name) self.name=Name end return Obj; end function CreateClassStudent(ID,Name,Class) local obj=CreateStudent(ID,Name); obj.class=4; function obj:GetClass() return self.class; end function obj.SetClass(self,class) self.class=class; end return obj; end s2=CreateClassStudent(1,"andy",5); print("s2‘class=",s2.GetClass(s2))
标签:style blog strong 数据 io 2014
原文地址:http://blog.csdn.net/shimazhuge/article/details/24836831