2017-06-03 28 views
1

我只是想知道为什么这回墨西哥不是英格兰?我一直试图通过它们的属性(如大陆)将x1分隔为x6,但我没有取得很大进展。任何人都知道很多关于lua表格,班级和功能?

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) 

回答

4

new上下文中,self是元表,并且o是该对象。在self上设置属性将覆盖以前的值。因此,改变

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

o.continent= continent or "" 
o.country = country or "" 
相关问题