任何人都对lua表,类和函数有很多了解吗?

时间:2017-06-03 14:28:20

标签: arrays function class lua lua-table

我只是想知道为什么这回到了墨西哥而不是英格兰?我一直在努力将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)

1 个答案:

答案 0 :(得分:4)

new的上下文中,self是metatable,o是对象。在self上设置属性会覆盖以前的值。所以,改变

self.continent= continent or ""
self.country = country or ""

o.continent= continent or ""
o.country = country or ""