如何将类实例作为参数传递给Lua中的成员函数

时间:2014-05-28 02:15:16

标签: lua lua-table

我正在尝试在lua中创建类,我需要将其作为参数传递给另一个类的成员函数实例。

-- inside class definition

function Calculator:add( numberClassInstanceWithAdditionDataEncapsulated )
  return numberClassInstanceWithAdditionDataEncapsulated:value();
end


-- numberClassInstanceWithAdditionDataEncapsulated is instance of my custom class SNum 
-- which contains methods and data and other stuffs, it is not number from language.
-- it doesn't build when I change value to showMyNumber ( bothfunctions are members 
-- of SNum class)

我收到错误

attempt to index local 'numberClassInstanceWithAdditionDataEncapsulated' (a number value)

如何将另一个类的实例传递给成员函数?

1 个答案:

答案 0 :(得分:0)

此错误只能在运行时发生,并且错误消息是明确的:numberClassInstanceWithAdditionDataEncapsulated是一个数字,无法编入索引。它不是一个表('对象'或类实例通常是表),如您所料。