是否可以在Codea中使用继承?虽然我对Lua相当陌生,但从一些快速的谷歌搜索来看,方法 to do 遗传和多态似乎有点"所涉及的“。有什么技术可以安全地使用Codea的Lua托管引擎吗?
这是一个简单的可运行的测试,我正在努力工作。我的超级阶级:
Superklass = class()
function Superklass:init(x,y)
self.x = x
self.y = y
end
function Superklass:debug()
print(string.format("(%d, %d)", self.x, self.y))
end子类:
Ship = class()
function Ship:init(x, y)
-- you can accept and set parameters here
print('ship:init() called')
self = Superklass(x,y) -- ???
print('attempting to call self:debug()')
self:debug() -- works! prints
print('ok!')
end
function Ship:draw()
print('ship:draw() called')
print('attempting to call self:debug()')
self:debug()
print('ok')
end该项目的切入点:
-- initial setup
function setup()
ship = Ship(HEIGHT/2, WIDTH/2)
end
-- called once every frame
function draw()
ship:draw()
end以下是运行该程序的输出:
ship:init() called
attempting to call self:debug()
(384, 375)
ok!
ship:draw() called
attempting to call self:debug()
error: [string "Ship = class()..."]:16: attempt to call method 'debug' (a nil value)
Pausing playback我相信这是难以置信的天真--但我希望能在Codea的上下文中找到一些有用的提示。
发布于 2012-04-29 20:32:06
给出一个可行的解决方案(Codea中的类继承),并指出其中的一些缺陷。首先,我应该注意,Codea按选项卡顺序加载类,因此超类的选项卡必须在子类的选项卡之前。我建议浏览一下Codea论坛上的这个帖子。它讨论了下面的技术,并给出了一些深入的基本机制。
首先,我们定义了一个超类AbstractSprite,其构造函数采用(x,y)坐标。它提供了一个方法调试将此坐标回显到控制台。
AbstractSprite = class()
function AbstractSprite:init(x,y)
self.position = vec2(x,y)
print("new AbstractSprite created")
end
function AbstractSprite:debug()
print(string.format("(%d,%d)", self.position.x, self.position.y))
end接下来,我们定义了一个实现类Ship,它实现了一个自定义的调试,它调用super,演示了如何在类层次结构上移动。
Ship = class(AbstractSprite)
function Ship:init()
AbstractSprite.init(self)
print("new Ship created")
end
function Ship:debug()
print("I am a ship, calling my superclasses' methods!")
AbstractSprite.debug(self)
end程序入口点。我们同时创建一个Ship和一个“raw”AbstractSprite,分别调用调试:
function setup()
ship = Ship()
ship:debug()
asteroid = AbstractSprite(150, 200)
asteroid:debug()
end和控制台输出:
new AbstractSprite created
new Ship created
I am a ship, calling my superclasses' methods!
(0,0)
new AbstractSprite created
(150,200)发布于 2012-04-28 22:19:35
免责声明:我是中产阶层的作者,它是Lua的OO。
你在第一个例子中指出的闭包是一个很好的智力练习,但它没有什么实际价值。在Lua中,使用表更好地实现面向对象(正如第二个示例所指出的那样)--它以更快的速度提供了等效的功能。唯一的区别是句法。
有许多库使用基于标准表的方法在Lua中执行OO。你可以在这里找到一份清单:
http://lua-users.org/wiki/ObjectOrientedProgramming
使用中间层,您的代码将变成如下所示:
require 'middleclass'
-- middleclass needs the class name as a parameter
SuperClass = class('SuperClass') -- I don't see the point of using a K here, BTW
function SuperClass:initialize(x,y) -- initialize instead of init
self.x = x
self.y = y
end
function SuperClass:debug()
print(string.format("(%d, %d)", self.x, self.y))
end
--
Ship = class('Ship', SuperClass) -- notice that we put the superclass here
function Ship:initialize(x, y) -- initialize instead of init again
-- you can accept and set parameters here
print('ship:initialize() called')
self = SuperClass.initialize(self,x,y) -- notice the extra self and initialize here
print('attempting to call self:debug()')
self:debug() -- works! prints
print('ok!')
end
function Ship:draw()
print('ship:draw() called')
print('attempting to call self:debug()')
self:debug()
print('ok')
endhttps://stackoverflow.com/questions/10367360
复制相似问题