【问题标题】:Anyone know much about lua tables,classes, and functions?有人知道很多关于 lua 表、类和函数的知识吗?
【发布时间】:2017-11-04 19:27:18
【问题描述】:

我只是想知道为什么这会返回墨西哥而不是英格兰?我一直在尝试通过它们的属性(例如大陆)将 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)

【问题讨论】:

    标签: arrays function class lua lua-table


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-18
      相关资源
      最近更新 更多