Smalltalk 是否有类接口的实现?
在 C# 中,类可以具有可以有多个实现的接口。在smalltalk中你如何做到这一点?
In C# classes can have interfaces that can have multiple implementations. How do you do that in smalltalk?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,您通常不需要接口,因为如果一个对象实现与另一个对象相同的消息,它就可以替换它。在 Java 和 C# 中,除非它们位于同一层次结构中,否则您无法执行此操作,因此您需要接口。
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.
今天与我的同事讨论后,在我看来,答案是任何类都可以被视为接口,因为任何类都可以在消息中传递给任何其他类。
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.
正如卢卡斯所说,大多数时候,你不需要它们。主要是因为要实现多态性,您唯一需要的就是实现相同的消息。无需为它们定义通用类型。
另一方面,有时,从我的角度来看,你确实需要接口。主要是当你有一个合同要显示时,或者当你有一种抽象超类时。这在开发框架时很常见。以记录器或序列化器为例。在这种情况下,您可能需要定义序列化器应实现的强制方法。然后您可以创建一个抽象超类,所有方法都以这种方式实现:
等等...所以,检查这个类,您现在必须实现哪些方法,以便该对象的用户正常工作。
请注意,
#shouldBeImplemented
是在Object
中实现的(在 Pharo Smalltalk 中):但正如您所看到的,这只是一个约定,并不是由语言本身。
干杯
马里亚诺
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:
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 inObject
with something like this (in Pharo Smalltalk):But as you can see, it is just a convention, it is not imposed by the language itself.
Cheers
Mariano
即使不称为“接口”,Dolphin Smalltalk (http://www.object-arts.com/) 也提供了名为“协议”的功能,这是一流的对象。
每个协议都定义了一组选择器(方法名称),您可以测试一个类是否符合某个协议:
您最终拥有一组正式/定义的方法名称,并且您可以检查某个对象是否可以使用在协议的背景下。
此外,类浏览器还将向您显示所选类所兼容的协议列表。
并且有一个协议浏览器,因此您可以探索每个协议并查看系统范围内哪些类符合它们。
总结:在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:
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.