Smalltalk 是否有类接口的实现?

发布于 2024-10-20 12:27:32 字数 50 浏览 2 评论 0原文

在 C# 中,类可以具有可以有多个实现的接口。在smalltalk中你如何做到这一点?

In C# classes can have interfaces that can have multiple implementations. How do you do that in smalltalk?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小嗲 2024-10-27 12:27:32

首先,您通常不需要接口,因为如果一个对象实现与另一个对象相同的消息,它就可以替换它。在 Java 和 C# 中,除非它们位于同一层次结构中,否则您无法执行此操作,因此您需要接口。

  • 在(所有)Smalltalk 中,都有协议(方法类别)用于将方法非正式地分组在一起。
  • Pharo Smalltalk 中有一些特征。乍一看它们看起来像接口,但它们也能够提供实现。

First of all you typically don't need interfaces, because if an object implements the same messages as another one it can replace it. In Java and C# you cannot do this unless they are in the same hierarchy, thus you need interfaces.

  • In (all) Smalltalk there are protocols (method categories) that serve the purpose of informally grouping methods together.
  • In Pharo Smalltalk there are Traits. At first they look like interfaces, but they are also able to provide an implementation.
南渊 2024-10-27 12:27:32

今天与我的同事讨论后,在我看来,答案是任何类都可以被视为接口,因为任何类都可以在消息中传递给任何其他类。

Smalltalk 中任意数量的类都可以响应相同的消息,因此您不需要 C# 和 java 中的接口。

After a discussion today with a coworker of mine it seems to me that the answer is any class could be considered an interface because any class can be passed in a message to any other class.

Any number of classes in smalltalk can respond to the same message, therefore you don't need interfaces as per C# and java.

笑着哭最痛 2024-10-27 12:27:32

正如卢卡斯所说,大多数时候,你不需要它们。主要是因为要实现多态性,您唯一需要的就是实现相同的消息。无需为它们定义通用类型。

另一方面,有时,从我的角度来看,你确实需要接口。主要是当你有一个合同要显示时,或者当你有一种抽象超类时。这在开发框架时很常见。以记录器或序列化器为例。在这种情况下,您可能需要定义序列化器应实现的强制方法。然后您可以创建一个抽象超类,所有方法都以这种方式实现:

LoggerIterface >> log: anObject
    self shouldBeImplemented


LoggerIterface >> reset
    self shouldBeImplemented

等等...所以,检查这个类,您现在必须实现哪些方法,以便该对象的用户正常工作。
请注意,#shouldBeImplemented 是在 Object 中实现的(在 Pharo Smalltalk 中):

Object >> shouldBeImplemented
    "Announce that this message should be implemented"

    self error: 'This message should be implemented'

但正如您所看到的,这只是一个约定,并不是由语言本身。

干杯

马里亚诺

As Lukas said, most of the times, you do not need them. Mostly because to achieve polymorphism, the only thing you need, is to implement the same message. There is no need to define a common type for them.

On the other hand, sometimes, from MY point of view, you do need interfaces. Mostly when you have a contract to show, or when having a kind of abstract superclass. This is very common when developing frameworks. Take as an example a logger or a serializer. In this case you may want to define the mandatory methods that a serializer should implement. Then you can create an abstract super class, with all the methods implemented this way:

LoggerIterface >> log: anObject
    self shouldBeImplemented


LoggerIterface >> reset
    self shouldBeImplemented

Etc...so, checking this class, you now which methods you have to implement so that the user of this object works well.
Notice that #shouldBeImplemented is implemented in Object with something like this (in Pharo Smalltalk):

Object >> shouldBeImplemented
    "Announce that this message should be implemented"

    self error: 'This message should be implemented'

But as you can see, it is just a convention, it is not imposed by the language itself.

Cheers

Mariano

虫児飞 2024-10-27 12:27:32

即使不称为“接口”,Dolphin Smalltalk (http://www.object-arts.com/) 也提供了名为“协议”的功能,这是一流的对象。

每个协议都定义了一组选择器(方法名称),您可以测试一个类是否符合某个协议:

conformsToProtocol: protocol
    "Answer whether the receiver conforms to the named <MethodProtocol>."

您最终拥有一组正式/定义的方法名称,并且您可以检查某个对象是否可以使用在协议的背景下。
此外,类浏览器还将向您显示所选类所兼容的协议列表。

并且有一个协议浏览器,因此您可以探索每个协议并查看系统范围内哪些类符合它们。

总结:在Smalltalk中接口不是必需的,至少不是实现多态性。然而,某些 Smalltalk 方言为协议提供了不同程度的支持,这些协​​议类似于接口,但适用于动态语言。

Even when not called "Interfaces", Dolphin Smalltalk (http://www.object-arts.com/) provides a feature named "Protocols" which are first class objects.

Each protocol defines a set of selectors (method names), and you can test whether a class conforms or not to a certain protocol:

conformsToProtocol: protocol
    "Answer whether the receiver conforms to the named <MethodProtocol>."

You end up having a formal/defined set of method names, and you can check whether certain object can be using in the context of a protocol.
Also, the class browser will show you the list of protocols to which the selected class is compliant.

And there is a protocol browser, so you can explore each protocol and view system-wide which classes conforms to them.

Summarizing: Interfaces are not necessary in Smalltalk, at least not to implement polymorphism. However certain Smalltalk dialects provide different degrees of support to protocols, which are the analog of Interfaces but for dynamic languages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文