问题描述
是否可以在Smalltalk中重载运算符?
Is it possible to overload operators in Smalltalk?
我正在寻找教程/示例.
I am looking for tutorials/examples.
谢谢.
推荐答案
在Smalltalk中,方法重载是不可能的.取而代之的是,方法重载和称为双重调度的技术的结合,用于实现与其他语言中的运算符重载相同的行为.
Method overloading is not possible in Smalltalk. Instead, a combination of method overriding and a technique called double dispatch is used to implement the same behavior as operator overloading in other languages.
您可以在数学运算符+,*,/,-
(在Smalltalk中为二进制消息)中找到示例实现.这里的想法是:Integer>>+
的实现向其参数发送一条消息#addWithInteger:
. #addWithInteger:
的实现是在每个Magnitude子类上实现的,以便专门添加Int + Int,Float + Int等...
You can find an example implementation in the mathematical operators +,*,/,-
(which are binary messages in Smalltalk). Here is the idea: the implementation of Integer>>+
sends a message #addWithInteger:
to its argument. The implementation of #addWithInteger:
is implemented on each Magnitude subclass, such as to specialize addition of Int+Int, Float+Int, etc...
这篇关于Smalltalk中的操作员可以超载吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!