Lua面向对象设计中添加super

来源:互联网 发布:泳衣走光 知乎 编辑:程序博客网 时间:2024/05/16 09:19

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

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


1.直接用父类调用

[java] view plaincopyprint?
  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 plaincopyprint?
  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 plaincopyprint?
  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 0
原创粉丝点击