繁体   English   中英

有人对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 ""

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM