1

getを使用してRebolで(実装継承)を持つことができると誰かが言った。だから私は試しました:

shape: context [
  x: 0
  y: 0
  draw: func['object][
    probe get object
  ]
]

circle: make shape [
  radius: 10
  draw: get in shape 'draw
]

rectangle: make shape [
  draw: get in shape 'draw
]

値ではなく参照でオブジェクトを渡したいので、'Object. しかし、私はそれをこのように呼ばなければなりません

circle/draw 'circle

通常の継承では、この種の不自然な構文を回避する this キーワードがありますが、名前の円を 2 回繰り返す必要があるため、これはかなり不自由です。もっとエレガントな方法はありますか?

ありがとう。

4

3 に答える 3

2

selfという言葉があります。それについて誤った確信を生み出す危険を冒して、おそらくあなたが望むことをする例を挙げます:

shape: make object! [
    x: 0
    y: 0
    draw: func [object [object!]] [
        probe object
    ]
]

circle: make shape [
    radius: 10
    draw: func [] [
        shape/draw self
    ]
] 

rectangle: make shape [
    draw: func [] [
        shape/draw self
    ]
]

ここでは、適切な「self」を使用して基本クラス関数を呼び出すことにより、引数をとらない関数を作成しました。

注意してください:他の言葉のように、それはバインドされます...そしてバインドがくっつきます. 抽象化を使い始めると、これは難しくなる可能性があります...

selfWordAlias: func [] [
    return 'self
]

triangle: make shape [
    draw: func [] [
        shape/draw get selfWordAlias
    ]
]

電話triangle/drawすると、おそらく驚かれることでしょう。あなたはオブジェクトメソッドにいて、selfWordAlias は単語「self」を返します。しかし、selfの概念は、グローバル システム コンテキストにある selfWordAlias が定義された時点でキャプチャされ、バインドされました。それがあなたが返すものです。

これに対処するためのツールがありますが、Rebol のスコーピングをしっかりと把握しておいてください。

于 2010-01-05T00:11:58.903 に答える
1

Rebolの典型的な継承を使用するだけで十分だと思いました。よりシンプルでエレガントです。

shape: make object!
  x: 0           
  y: 0
  draw: func [] [probe self]
]

circle: make shape [
  radius: 10
]

triangle: make shape []

次の結果が得られます。

>> shape/draw
make object! [
    x: 0
    y: 0
    draw: func [][probe self]
]
>> circle/draw            
make object! [
    x: 0
    y: 0
    draw: func [][probe self]
    radius: 10
]
>> triangle/draw          
make object! [
    x: 0
    y: 0
    draw: func [][probe self]
]
于 2010-01-15T08:22:38.163 に答える
1

すべてのオブジェクトに同じ関数 (関数のコピーではない) を使用する場合は、この方法を使用します。別のオブジェクトをコピーする場合でも、オブジェクトを作成するたびに関数をポイントします。

>> f: does [print "hello"]
>> o: context [a: 1 b: :f]
>> o/b
hello
>> p: context [a: 2 b: :f]
>> p/b
hello

>> same? get in o 'b get in p 'b
== true ;they are exactly the same function

>> append last second :f " world!" ;change the function
== "hello world!"
>> o/b
hello world!
>> p/b
hello world!

もちろん、バインディングの問題に注意する必要があります。これが役立つことを願っています。

于 2010-07-27T07:55:17.000 に答える