【问题标题】:Shared function in Rebol (implementation inheritance)Rebol 中的共享函数(实现继承)
【发布时间】:2023-04-02 22:58:02
【问题描述】:

有人说你可以使用 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

这是相当蹩脚的,因为我需要重复两次名称 circle 而在通常的继承中,有 this 关键字可以避免这种不自然的语法。有没有更优雅的方式?

谢谢。

【问题讨论】:

    标签: rebol


    【解决方案1】:

    有一个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 时被捕获和绑定,这是在全局系统上下文中。所以这就是你得到的回报。

    有一些工具可以解决这个问题,但请确保您牢牢掌握Scoping in Rebol

    【讨论】:

    • 会考虑的,我已经和Rebol断开了一段时间,只需要回来:)
    【解决方案2】:

    我原以为只需要使用 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]
    ]
    

    【讨论】:

    • 是的,我知道原型继承。但它复制了绘图功能。关键不是复制而是共享它,这样如果您更改共享功能,它会在任何地方自动更改。
    【解决方案3】:

    当我想对所有对象使用相同的函数(而不是函数的副本)时,我会使用此方法。 每当你创建一个对象时就指向这个函数,即使你复制另一个对象。

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

    当然,您应该注意绑定问题。希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-23
      • 1970-01-01
      • 1970-01-01
      • 2012-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多