Lua面向对象设计中添加super

云风的lua oo 实现方法 http://blog.codingnow.com/2006/06/oo_lua.html

无法在子类的函数中使用super:xxx 调用父类的函数。


1.直接用父类调用

[java]  view plain copy print ?
  1. base_type=class()        -- 定义一个基类 base_type  
  2. function base_type:ctor(x)    -- 定义 base_type 的构造函数  
  3.     print("base_type ctor")  
  4.     self.x=x  
  5. end  
  6. function base_type:print_x()    -- 定义一个成员函数 base_type:print_x  
  7.     print(self.x)  
  8. end  
  9. function base_type:hello()    -- 定义另一个成员函数 base_type:hello  
  10.     print("hello base_type")  
  11. end  
  12.   
  13. test=class(base_type)    -- 定义一个类 test 继承于 base_type  
  14. function test:ctor()    -- 定义 test 的构造函数  
  15.     print("test ctor")  
  16. end  
  17. function test:hello()    -- 重载 base_type:hello 为 test:hello  
  18.     base_type.hello(self)  
  19.     print("hello test")  
  20. end  
  21.   
  22. test1=class(base_type)    -- 定义一个类 test 继承于 base_type  
  23.   
  24. local test1obj = test1.new()  
  25. test1obj:hello()  


2. 

[javascript]  view plain copy print ?
  1. local _class={}  
  2.   
  3. function class(super)  
  4.     local class_type={}  
  5.     class_type.ctor=false  
  6.     class_type.super=super  
  7.     class_type.new=function(...)  
  8.             local obj={}  
  9.             do  
  10.                 local create  
  11.                 create = function(c,...)  
  12.                     if c.super then  
  13.                         create(c.super,...)  
  14.                     end  
  15.                     if c.ctor then  
  16.                         c.ctor(obj,...)  
  17.                     end  
  18.                 end  
  19.   
  20.                 create(class_type,...)  
  21.             end  
  22.             --obj.super = _class[super]--1  
  23.             setmetatable(obj,{ __index=_class[class_type] })  
  24.             return obj  
  25.         end  
  26.     local vtbl={}  
  27.         vtbl.super = _class[super]--2  
  28.         _class[class_type]=vtbl  
  29.   
  30.     setmetatable(class_type,{__newindex=  
  31.         function(t,k,v)  
  32.             vtbl[k]=v  
  33.         end  
  34.     })  
  35.   
  36.     if super then  
  37.         setmetatable(vtbl,{__index=  
  38.             function(t,k)  
  39.                 local ret=_class[super][k]  
  40.                 vtbl[k]=ret  
  41.                 return ret  
  42.             end  
  43.         })  
  44.     end  
  45.   
  46.     return class_type  
  47. end  

目前我只想到在new函数中插一句obj.super = _class[super]

后来又想到可以把super放vtbl中,vtbl.super = _class[super]。这样占用空间少点,不用每个对象设置super。不过改变了一个既定规则,就是vtbl中都是函数。

使用方法:self.super.xxx(self,....)。不能采用self.super:xxx()。

[java]  view plain copy print ?
  1. function test:hello()   -- 重载 base_type:hello 为 test:hello  
  2.     self.super.hello(self)  
  3.     print("hello test")  
  4. end  

假如test1 继承 test ,test 继承base_type。test1没定义hello,调用test1obj:hello()。

_class[test1]  =  vbtl,  这个vbtl.super  是_class[test]

但是结果却不如愿,因为这里self是test1的对象,test1没定义hello。

于是test1的vbtl去super 找,于是slef.super是test,而不是我们所期望的base_type。

test:hello()里面套了test:hello(),就形成无限循环崩掉了。

3.

super不应该从属于对象,而应该是类。所以self.super应该改成test.super。

test.super其实就是class_type.super,已经被占用了。只能另外用class_type.superclass了。

class()定义中加入  class_type.superclass = _class[super]


调用时为  test.superclass.hello(self)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Lua ,可以使用“似有字段”(Pseudo-Fields)来模拟面向对象编程的成员变量。所谓“似有字段”,指的是在表使用函数来表示一个字段,使得它看起来像一个变量,但实际上是一个函数调用。下面是一个使用“似有字段”实现面向对象的示例: ```lua -- 定义一个类 local MyClass = {} -- 定义一个似有字段 name MyClass.name = function(self, value) if value then self._name = value else return self._name end end -- 定义一个方法 function MyClass:hello() print("Hello, my name is " .. self:name() .. "!") end -- 创建一个实例 local obj = {} setmetatable(obj, { __index = MyClass }) -- 使用 name 字段 obj:name("Jack") print(obj:name()) -- 输出 "Jack" -- 调用方法 obj:hello() -- 输出 "Hello, my name is Jack!" ``` 在上面的示例,我们定义了一个类 `MyClass`,并在其使用了一个似有字段 `name`。这个似有字段实际上是一个函数,可以用来设置和获取对象的名称。 然后,我们在类定义了一个方法 `hello`,用来输出对象的名称。在方法,我们调用了 `self:name()` 来获取对象的名称,实际上是调用了 `MyClass.name(self)` 函数来获取值。 最后,我们创建了一个实例 `obj`,并使用 `obj:name()` 来设置和获取对象的名称,以及调用 `obj:hello()` 方法来输出对象的名称。 通过使用“似有字段”,我们可以模拟面向对象编程的成员变量,并在方法使用它们。需要注意的是,在 Lua 没有真正的私有变量,所以使用“似有字段”时需要注意保护对象的数据,以避免被外部直接修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值