是“呼叫者”吗? 在Java中与“接收者”相同。 在鲁比?

发布于 2024-07-23 09:26:24 字数 229 浏览 2 评论 0原文

如果我说

x.hello()

在 Java 中,对象 x 正在“调用”它包含的方法。

在 Ruby 中,对象 x 正在“接收”它包含的方法。

这只是表达相同想法的不同术语还是这里的意识形态存在根本差异?

来自 Java 的我发现 Ruby 的“接收者”想法非常令人困惑。 也许有人可以用 Java 来解释这一点?

If I say

x.hello()

In Java, object x is "calling" the method it contains.

In Ruby, object x is "receiving" the method it contains.

Is this just different terminology for expressing the same idea or is there a fundamental difference in ideology here?

Coming from Java I find Ruby's "receiver" idea quite baffling. Perhaps someone could explain this in relation to Java?

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

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

发布评论

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

评论(3

少女的英雄梦 2024-07-30 09:26:24

在您的示例中,x 没有调用hello()。 任何包含该片段的对象都是“调用者”(即,它是“调用者”)。 在Java中,x可以称为接收者; 它正在接收对 hello() 方法的调用。

In your example x is not calling hello(). Whatever object contains that snippet is "calling" (i.e., it's the "caller"). In Java, x can be referred to as the receiver; it is receiving the call to the hello() method.

以为你会在 2024-07-30 09:26:24

差异不仅仅在于术语。 在Java 中,VM 确定给定对象是否“接受”您尝试发送的消息(即您尝试调用的方法)。 如果对象的类型空间没有定义该方法,则会引发异常并且永远不会传递消息。

在 Ruby 中,消息总是传递。 该对象可能会找到与其匹配的方法,也可能找不到,在后一种情况下,它可能会抛出异常,也可能不会。 Rails 正是建立在这一根本差异之上的。 这就是为什么在 Java 平台上还没有像 Rails 那样有用的数据库支持的 Web 应用程序框架(尽管有些框架已经接近)的原因之一。

The difference is more than terminology. In Java, the VM determines whether a given object "accepts" the message that you're trying to send (i.e., the method you're trying to call). If the object's type space doesn't define that method, an exception is thrown and the message is never delivered.

In Ruby, the message is always delivered. The object may find a method that matches it, or it may not, and in the latter case it may throw an exception, or it may not. Rails is built on this fundamental difference. It's one of the reasons why there isn't a DB-backed web app framework as useful as Rails on the Java platform yet (though some are getting close).

时光与爱终年不遇 2024-07-30 09:26:24

如果我错了,有人会纠正我,但我认为你不能将这些术语应用于 Java。 Ruby 来自 Smalltalk,它使用消息(而不是方法)在对象之间进行通信。 从技术上讲,当您在 Ruby 中执行 myObj.to_s 时,您正在向 myObj 发送 to_s 消息,并且它会相应地对该消息进行操作。 使用该模型,myObj 确实是此消息的接收者,而拥有发送消息的行的类是发送者。

在Java中,这是不存在的。 您有可以调用方法的对象。 没有发送者和接收者。 当你说意识形态存在根本差异时,你说得对。

Someone correct me if I'm wrong, but I don't think you can apply these terms to Java. Ruby comes from Smalltalk, which uses messages (not methods) to communicate between objects. Technically, when you do myObj.to_s in Ruby, you are sending the to_s message to myObj and it is acting upon that message accordingly. With that model, myObj is indeed the receiver of this message and the class that owns the line where the message was sent is the sender.

In Java, this doesn't exist. You have objects that you call methods on. There are no senders and receivers. You had it right when you said there is a fundamental difference in ideology.

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