Lua中元方法的继承 [英] Inheritance for metamethods in Lua

查看:143
本文介绍了Lua中元方法的继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常喜欢在lua编程中描述面向对象编程16.1,16.2:

i very much like how object oriented programming is described in "programming in lua" 16.1, 16.2:

http://www.lua.org/pil/16.1.html

< a href =http://www.lua.org/pil/16.2.html\"rel =noreferrer> http://www.lua.org/pil/16.2.html

并希望遵循这种方法。但我想更进一步:我希望有一个基类类,称为类,它应该是所有子类的基础,因为我想在那里实现一些辅助方法(如instanceof等。),但基本上它应该如书中所述:

and would like to follow this approach. but i would like to take things a little further: i would like to have a base "class" called "class", which should be the base of all subclasses, because i want to implement some helper methods there (like "instanceof" etc.), but essentially it should be as described in the book:

function class:new(o)
    o = o or {}
    setmetatable(o, self)
    self.__index = self
    return o
end

现在我的问题:

我希望有一个数字类,它继承自类 :

i would like to have a class "number", which inherits from "class":

number = class:new()

我想在这个类中为运算符重载(__add,__sub等)定义元方法,所以类似于:

i would like to define metamethods for operator overloading (__add, __sub, etc.) in this class, so something like:

n1 = number:new()
n2 = number:new()

print(n1 + n2)

有效。这不是一个真正的问题。但现在我想要第三类钱,继承自数字:

works. this is not really a problem. but now i would like to have a third class "money", which inherits from "number":

money = number:new{value=10,currency='EUR'}

我在这里介绍一些新属性等。

i introduce some new properties here and such.

现在我的问题是,我没有工作,钱继承了class和number的所有方法,包括在数字中定义的所有元方法。

now my problem is, that i don't get things to work, that "money" inherits all methods from "class" and "number" including all metamethods defined in "number".

我尝试了几种覆盖新或修改元数据但我无法使用的东西,没有丢失钱中的类方法或者丢失钱中数字的元方法

i've tried several things like overwriting "new" or modifying metatables but i was not able to get things to work, without either loosing the methods of "class" in "money" or loosing the metamethods of "number" in "money"

我知道,有很多类的实现在那里,但我真的想坚持lua本身的最小方法。

i know, that there a lot's of class implementations out there, but i would really like to stick with the minimal approach of lua itself.

任何帮助都将非常感谢!

any help would be very much appreciated!

非常感谢!

推荐答案

我认为你的问题是因为使用与 <类似的东西查找运算符元方法的事实code> rawget(getmetatable(obj)或{},__ add) 。因此,运算符不会与其他函数一起继承。

I think that the problem you are having is due to the fact the the operator metamethods are looked up using something similar to rawget(getmetatable(obj) or {}, "__add"). Thus the operators are not inherited with along with the other functions.

我已经取得了一些成功的 new 函数复制像这样的运算符:

I have had some success with having the new function copy the operators like this:

function class:new(o)
    o = o or {}
    setmetatable(o, self)
    self.__index = self
    local m=getmetatable(self)
    if m then
        for k,v in pairs(m) do
            if not rawget(self,k) and k:match("^__") then
                self[k] = m[k]
            end
        end
    end
    return o
end

这篇关于Lua中元方法的继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆