为什么Java可以接受多个参数但只能返回一个对象?

发布于 2024-08-24 23:34:39 字数 83 浏览 5 评论 0原文

为什么 java 的定义方法可以接受多个参数作为输入,
但可能只返回单个对象(或 void)?

它是否使该语言更易于实现或使用?

Why was java defined such that methods may take as input multiple parameters,
but may only return a single object (or void)?

Did it make the language somehow easier to implement or use?

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

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

发布评论

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

评论(4

回眸一笑 2024-08-31 23:34:39

可能是因为 C 和 C++ 就是这样做的,而 Java 语言的语法与这些语言非常相似(并且可能基于)。

事实上,根据这篇文章, Gosling 首先扩展了 C++ 编译器,因此他遵循大部分相同的语法是有道理的:

为了使开发过程更加平台中立(从而满足消费市场对 CPU 灵活性的需求),Gosling 首先扩展了 C++ 编译器。

Probably because this is the way that C and C++ do it, and the Java language syntax is very similar to (and probably based on) those languages.

In fact, according to this article, Gosling started by extending the C++ compiler, so it makes sense that he would follow much of the same syntax:

To make development a more platform-neutral process (and thus accommodate the consumer market's demand for CPU flexibility), Gosling began by extending the C++ compiler.

蓝戈者 2024-08-31 23:34:39

也许意图是将多个返回值封装在一个对象中?

Maybe the intention was that multiple return values be encapsulated in an object?

岁月流歌 2024-08-31 23:34:39

我不确定,但我想 Java 的执行方式就像任何其他基于堆栈的运行时一样。这意味着将项目作为参数传递到方法中,只需在将控制权转移到方法之前将它们推入堆栈即可轻松完成。返回值可能像 C 和 C++ 一样在 VM 中处理 - 返回值始终放置在寄存器中,寄存器本质上是单值。

不过,这并不是一个大问题,因为使用泛型,可以通过返回类似 Tuple 这样的实例,以类型安全的方式处理返回多个值,这不是'在大多数情况下,这并不算太大的负担。

I don't know for sure, but I imagine that Java is executed like any other stack-based runtime. Which means that passing items as parameters into a method is done easily by simply pushing them onto the stack before transferring control to the method. Return values are probably handled in the VM like C and C++ do - the return value is always placed in a register, which is by nature single-valued.

It's not a big problem, though, because with generics, returning multiple values can be handled in a type-safe way by returning an instance of something like Tuple<type1, type2, type3>, which isn't too great a burden to bear in most cases.

玻璃人 2024-08-31 23:34:39

我的猜测是约定:这就是主要的面向对象语言所做的事情,所以这就是 Java 所做的事情。然而,官方的论点可能与关于闭包的论点相同——不包括那些会混淆普通任务和非真正需要的任务的标准语法的东西。或者也许他们认为单个方法应该返回一组应该/可以进入对象的相关值。或者也许他们只是没有想到/没有时间(相当标准的开发人员费用)。

在相关说明中,我发现了有关使用闭包模拟多个返回的讨论:
http://weblogs.java.net/blog/brucechapman/存档/2007/11/closures_and_mu.html

My guess would be convention: that's what the major OO languages do/did, so that's what Java did. However, the official argument would probably be the same as the one about closures - don't include things that will cloud up the standard syntax for mundane tasks with not-really-needed tasks. Or maybe they figured that a single method should be returning a set of related values that should/could go into an object. Or maybe they just didn't think of it/have the time (pretty standard developer fare).

On a related note, I found this discussion of using closures to mock multiple returns:
http://weblogs.java.net/blog/brucechapman/archive/2007/11/closures_and_mu.html

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