2014-10-18 42 views
1

所以我爲Lua創建了一個系統,所以我可以在其中創建類和對象。我創建了對象,問題是創建構造函數。在Lua中使用對象

我有構造是這樣的:

a = MyClass:Create("Hello World!") 

的創建方法......作爲參數它傳遞到構造方法(OnStart中)。我可以在Create方法中讀取參數,但是當調用OnStart時,參數總是以nil代替「Hello World!」。

我的代碼:

Object = { } 

function Object:Create(...) 
    local instance = { } 
    setmetatable(instance, self) 
    self.__index = self 
    instance.Type = Object 

    -- Now we can call the constructor. 
    local arg = { ... } 
    instance.OnStart(table.unpack(arg)) 

    return instance 
end 

function Object:OnStart(msg) 
    print(msg) 
end 

test = Object:Create("Hello World!") 
print(test:ToString()) 

一些如何在這裏的msg參數最終被零...

回答

1

看來我已經找到了爲什麼它不工作時,輕微的細節需要改爲一行9.而不是instance.OnStart它需要是instance:OnStart