|
于 2012-6-18 17:06, Stephen Virgo 写道:
sorry about that. the code in my last post is incorrect. one possible working __index function might be like this: ---------------------------- code begine ------------------------------------- --- -- the __index meta method will do the following: -- firstly get the indexed field of the wrapped object(i.e. the proto) -- if the field is not a method(i.e. function), just return it -- else return a wrapped function to call the method on the wrapped object -- note however if the field indexed is just retrived but not called on -- the wrapper object, unwanted behavior might occur. function game_wrapper_mt:__index(key) local proto = rawget(self, "__proto__") local field = proto and proto[key] if type(field) ~= "function" then return field else return function (obj, ...) if obj == self then return field(proto, ...) else return field(obj, ...) end end end end ----------------------------- code end --------------------------------------- best regards. |