OO in lua trouble: inherit

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

OO in lua trouble: inherit

Andy.Tao
Hi gus,

In my practice, I found two very strange behavior(please see attachment),  the detail as follow:
client.lua: line 195: attempt to call method 'Debug' (a nil value)
question: why it could not find the method?

client_xchat.lua: line 83: attempt to index local 'tblSvrPacket' (a number value)
question: the parameter is table, why it become the number?

could you give me some advice to solve this problem, thanks in advanced.
 
andy.

------------------ 原始邮件 ------------------
发送时间: 2011年5月6日(星期五) 上午10:09
收件人: "lua-l"<[hidden email]>; "roberto"<[hidden email]>;
主题: OO in lua new style: inherit
 
Hi guys,

I have found a new OO style,  it is very convenient, so share it for u, any suggestion is welcome.
===============================================================
Client = {
    __debug = false,                -- 是否调试模式
    __online = false,               -- 是否联机在线
    __timeout = 5000,               -- 默认超时时间
}


function Client:New()
    object = {}

    -- 对象方法继承
    setmetatable(object, self)
    self.__index = self

    -- 对象属性继承
    object.__debug = self.__debug
    object.__online = self.__online
    object.__timeout = self.__timeout

    return object
end

function Client:Delete()
    -- release resources
end

function Client:Print(...)
    print("Client: ", unpack(arg))
end

-- 继承基类对象
CLIENT_XCHAT = Client:New()

function CLIENT_XCHAT:New()
    object = {}

    -- 对象方法继承
    setmetatable(object, self)
    self.__index = self

    return object
end

function CLIENT_XCHAT:Release()
    self:Delete()
end

function CLIENT_XCHAT:Log(...)
    self:Print("CLIENT_XCHAT: ", unpack(arg))
end

-- Example
myClient = CLIENT_XCHAT:New()
myClient:Log("Hello world")


------------------
Andy Tao[陶祖洪]
祖洪测试自动化 http://www.zuhong.cn
天是圆的,地是方的,凡事都要有个规矩!
 

client.lua (11K) Download Attachment
client_xchat.lua (4K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: OO in lua trouble: inherit

Fabian Peña
I think that you call both methods using "." instead use ":"

CLIENT_XCHAT._Match({}, 8) -- missing self, {} is self now and 8 is tblSvrPacket

Client._Observe({})   -- missing self in the call , {} is self now and Debug method non exist.

Regards Fabian.
Sorry, bad bad english.


On 06/05/2011 10:51 p.m., 陶陶 wrote:
Hi gus,

In my practice, I found two very strange behavior(please see attachment),  the detail as follow:
client.lua: line 195: attempt to call method 'Debug' (a nil value)
question: why it could not find the method?

client_xchat.lua: line 83: attempt to index local 'tblSvrPacket' (a number value)
question: the parameter is table, why it become the number?

could you give me some advice to solve this problem, thanks in advanced.
 
andy.

------------------ 原始邮件 ------------------
发送时间: 2011年5月6日(星期五) 上午10:09
收件人: "lua-l"[hidden email]; "roberto"[hidden email];
主题: OO in lua new style: inherit
 
Hi guys,

I have found a new OO style,  it is very convenient, so share it for u, any suggestion is welcome.
===============================================================
Client = {
    __debug = false,                -- 是否调试模式
    __online = false,               -- 是否联机在线
    __timeout = 5000,               -- 默认超时时间
}


function Client:New()
    object = {}

    -- 对象方法继承
    setmetatable(object, self)
    self.__index = self

    -- 对象属性继承
    object.__debug = self.__debug
    object.__online = self.__online
    object.__timeout = self.__timeout

    return object
end

function Client:Delete()
    -- release resources
end

function Client:Print(...)
    print("Client: ", unpack(arg))
end

-- 继承基类对象
CLIENT_XCHAT = Client:New()

function CLIENT_XCHAT:New()
    object = {}

    -- 对象方法继承
    setmetatable(object, self)
    self.__index = self

    return object
end

function CLIENT_XCHAT:Release()
    self:Delete()
end

function CLIENT_XCHAT:Log(...)
    self:Print("CLIENT_XCHAT: ", unpack(arg))
end

-- Example
myClient = CLIENT_XCHAT:New()
myClient:Log("Hello world")


------------------
Andy Tao[陶祖洪]
祖洪测试自动化 http://www.zuhong.cn
天是圆的,地是方的,凡事都要有个规矩!