Next: , Previous: , Up: The mini-oof.fs model   [Contents][Index]


6.24.5.2 Mini-OOF Example

短い例で、 このパッケージの使い方を示します。 他の例として button オブジェクト の Mini-OOF バージョンが moof-exm.fs にあります。

object class
  method init
  method draw
end-class graphical

このコードは、 draw 操作を持つクラス graphical を定義します。 任意の graphical オブジェクトに対して draw 操作を実行できます。例:

100 100 t-rex draw

ここで、 t-rex はオブジェクトまたはオブジェクト・ポインターであり、 たとえば次のように作成されます: graphical new Constant t-rex

具体的な graphical オブジェクトのために、 クラス graphical の子クラスを定義します。 例:

graphical class
  cell var circle-radius
end-class circle \ "graphical" is the parent class

:noname ( x y -- )
  circle-radius @ draw-circle ; circle defines draw
:noname ( r -- )
  circle-radius ! ; circle defines init

暗黙的な init メソッドはないため、 明示的に定義する必要があります。 オブジェクトの作成コードは明示的に init を呼び出す必要があります。

circle new Constant my-circle
50 my-circle init

すべてのオブジェクトの同じ場所に init がある場合、 init の自動呼び出しで名前付きオブジェクトを作成する関数を追加することもできます:

: new: ( .. o "name" -- )
    new dup Constant init ;
80 circle new: large-circle

以下のようにして、 この新しい circle を (100,100) に描画できます:

100 100 my-circle draw