Codeaで継承を使用できますか?私はかなりLuaに慣れているが、いくつかのグーグルグーグルでは、the methodsのように見えます。inheritance and polymorphismはビット "involved"です。 CodeaのLuaホスティングエンジンで安全に使用できる手法はありますか?Codeaで継承を使用できますか?
ここでは、私が働くことを試みている簡単な実行可能なテストです。私のスーパークラス:
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の文脈で動くかもしれない何かの方向性についてのヒントを愛するでしょう。
CODEAは必ずしもアルファベット順にファイルをロードしません: –
@MatthewMurdochありがとう!更新しました –