有人对 lua 表、类 和函数了解很多吗?
Anyone know much about lua tables,classes, and functions?
我只是想知道为什么返回墨西哥而不是英格兰?我一直在尝试通过它们的属性将 x1 到 x6 分开,比如大陆,但我没有取得太大进展。
world = {continent = "", country = ""}
function world:new (o,continent,country)
o = o or {}
setmetatable(o, self)
self.__index = self
self.continent= continent or ""
self.country = country or "";
return o
end
x1 = world:new(nil,"Europe","England")
x2 = world:new(nil,"Europe","France")
x3 = world:new(nil,"Africa","Algeria")
x4 = world:new(nil,"Africa","Nigera")
x5 = world:new(nil,"America","United States")
x6 = world:new(nil,"America","Mexico")
list_1 = {x1,x2,x3,x4,x5,x6}
print(list_1[1].country)
在new
的上下文中,self
是元表,o是对象。在 self
上设置属性会覆盖以前的值。所以,改变
self.continent= continent or ""
self.country = country or ""
至
o.continent= continent or ""
o.country = country or ""
我只是想知道为什么返回墨西哥而不是英格兰?我一直在尝试通过它们的属性将 x1 到 x6 分开,比如大陆,但我没有取得太大进展。
world = {continent = "", country = ""}
function world:new (o,continent,country)
o = o or {}
setmetatable(o, self)
self.__index = self
self.continent= continent or ""
self.country = country or "";
return o
end
x1 = world:new(nil,"Europe","England")
x2 = world:new(nil,"Europe","France")
x3 = world:new(nil,"Africa","Algeria")
x4 = world:new(nil,"Africa","Nigera")
x5 = world:new(nil,"America","United States")
x6 = world:new(nil,"America","Mexico")
list_1 = {x1,x2,x3,x4,x5,x6}
print(list_1[1].country)
在new
的上下文中,self
是元表,o是对象。在 self
上设置属性会覆盖以前的值。所以,改变
self.continent= continent or ""
self.country = country or ""
至
o.continent= continent or ""
o.country = country or ""