2012-04-28 18 views
0

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の文脈で動くかもしれない何かの方向性についてのヒントを愛するでしょう。

答えて

3

だけで作業溶液(CODEAでクラスの継承)を与え、いくつかを指摘します落とし穴のまず、Codeaがクラスをタブ順にロードするので、スーパークラスのタブはのサブクラスのタブの前にでなければなりません。私はthis thread on the Codea forumsを見てお勧めします。以下のテクニックについて説明し、基本的な仕組みについてのいくつかの洞察も提供します。

まず、コンストラクタが(x、y)座標をとる抽象クラスAbstractSpriteを定義します。この座標をコンソールにエコーする方法debugを提供します。

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 = 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 

プログラムのエントリポイント。私たちは、それぞれにデバッグを呼び出すと、と '生の' 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) 
+1

CODEAは必ずしもアルファベット順にファイルをロードしません: –

+0

@MatthewMurdochありがとう!更新しました –

2

免責事項:私はの著者であり、Lua用のOO-libです。

最初の例で指摘したクロージャベースは素晴らしい知的エクササイズですが、実用的価値はほとんどありません。 Luaでは、テーブルの方がオブジェクトの方位が優れています(2番目の例で指摘されている例のように)。より速い速度で同等の機能を提供します。唯一の違いは構文的です。

標準的なテーブルベースのアプローチを使用してLuaでオブジェクト指向を行うライブラリはたくさんあります。あなたがここにリストを見つけることがあります。

中流階級を使用して

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') 
end 
+0

ありがとうを!あなたはCodeaの文脈でこれを使ってみたことがありますか? –

+1

ただ、将来のGooglerのために、私はかなりよく、それをレイアウトされ、これを発見した:http://twolivesleft.com/Codea/Talk/discussion/96/class-inheritances/p1 –

+0

を、私は申し訳ありませんが、CODEAそれを試していません。 – kikito

関連する問題