Rebol 中的共享函数(实现继承)
有人说你可以使用 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.name' 传递名称。但是我必须这样称呼它,
circle/draw 'circle
这是相当蹩脚的,因为我需要重复名称循环两次,而在通常的继承中,有 this 关键字可以避免这种不自然的语法。有更优雅的方式吗?
谢谢。
Someone said you can have (implementation inheritance) with Rebol using get. So I tried:
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
]
I want to pass the object by reference not by value so I pass only the name using 'Object. But then I have to call it like this
circle/draw 'circle
which is rather lame as I need to repeat the name circle twice while in usual inheritance there is the this keyword which avoid this kind of unatural syntax. Is there a more elegant way ?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个
self
词。冒着对此产生错误的确定感的风险,我将给您一个示例,大概可以实现您想要的功能:调用基类函数来创建采用零参数的函数
这里,我们通过使用适当的“self” < b>当心:就像其他词一样,它会被绑定......并且绑定会粘住。一旦您开始使用抽象,这可能会变得棘手......
调用
triangle/draw
可能会让您感到惊讶。您在对象方法中,selfWordAlias 返回单词“self”。但是 self 的概念是在定义 selfWordAlias 时捕获并绑定的,它位于全局系统上下文中。这就是你得到的回报。有一些工具可以处理这个问题,但请确保您牢牢掌握Rebol 中的范围界定 !
There is a
self
word. At the risk of creating a false sense of certainty about that, I'll give you an example that presumably does what you want:Here we've made functions that take zero arguments by calling the base class function with the appropriate "self"
Beware: like other words, it gets bound...and the binding sticks. This can get tricky once you start working with abstractions...
Calling
triangle/draw
will probably surprise you. You're in the object method and selfWordAlias returns the word "self". But the notion of self was captured and bound at the time the selfWordAlias was defined, which was in the global system context. So that's what you get back.There are tools for dealing with this, but make sure you've got a firm grip on Scoping in Rebol !
我本以为使用 Rebol 的原型继承就足够了。它更简单、更优雅:
给出以下结果:
I would have thought that using Rebol's prototypical inheritance is all that you need. It is simpler and more elegant:
Which give the following results:
当我想对所有对象使用相同的函数(而不是函数的副本)时,我使用此方法。
每当您创建对象时,即使复制另一个对象,也指向该函数。
当然,您应该注意绑定问题。希望这会有所帮助。
I use this method when I want to use same function (not the copies of function) for all my objects.
Point the function whenever you create an object, even if you copy another object.
Ofcourse you should take care about the binding issues. Hope this will help.