我只是想知道为什么这回到了墨西哥而不是英格兰?我一直在努力将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)
答案 0 :(得分:4)
在new
的上下文中,self
是metatable,o是对象。在self
上设置属性会覆盖以前的值。所以,改变
self.continent= continent or ""
self.country = country or ""
到
o.continent= continent or ""
o.country = country or ""