Java 中 C# 的 ref 和 out

发布于 2024-10-07 09:49:07 字数 201 浏览 3 评论 0原文

众所周知,两种语言在将参数传递给方法时都是按值传递的。但 C# 支持 refout 关键字来传递基本类型的引用。我正在 Java 中寻找相同的关键字和技术?

我的猜测是使用Integer包装类而不是Java中的int来传递。

有什么建议和例子吗?

As we know both language are pass-by-value when passing parameters to methods. But C# supports ref and out keywords to pass-by-reference of primitive types. I am looking for the same keywords and technique in Java?

My guess is using Integer wrapper class instead of int in Java to pass in.

Any suggestions and examples?

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

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

发布评论

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

评论(6

疑心病 2024-10-14 09:49:07

你的猜测是正确的。需要一个包装器(但不需要 Integer,因为它是不可变的)。

有些人使用单元素数组来达到此目的:

int[] x = { 0 };
int[] y = { 0 };
someMethod(x, y);
return x[0] + y[0];

许多人会将该技术与 GOTO 放在一起。

有些人定义通用持有者类:

public class Holder<T> {
    private T _value;
    private Holder(T value) { _value = value; }
    public static of(T value) { return new Holder<T>(value); }
    public T getValue() { return _value; }
    public void setValue(T value) { _value = value; }
}

...

Holder<String> x = Holder.of("123");
Holder<String> y = Holder.of("456");
someMethod(x, y);
return x.getValue() + y.getValue();

有些人定义专用类型:

SomeMethodResult result = someMethod(x, y);
return result.getX() + result.getY();

有些人会安排在方法内部完成工作,首先避免对引用参数的需要:

return someMethod(x, y);

这些技术中的每一种都有优点和缺点:

  • 数组:简单与丑陋,依赖于只有一个元素
  • 持有者的数组:安全与冗长,拳击
  • 专用类型:安全与冗长,可能过度杀伤
  • 改变方法:安全,干净与并不总是可能

就我个人而言,我认为Java 在这方面搞砸了。我宁愿避免通过引用参数,但我希望 Java 允许一个方法有多个返回值。但说实话,我并不经常被这个问题绊倒。我不会为了这个功能而付出肾脏。 :)

Your guess is correct. A wrapper is needed (but not Integer as it is immutable).

Some people use single element arrays for this purpose:

int[] x = { 0 };
int[] y = { 0 };
someMethod(x, y);
return x[0] + y[0];

Many will rank that technique right up there with GOTO.

Some people define a generic holder class:

public class Holder<T> {
    private T _value;
    private Holder(T value) { _value = value; }
    public static of(T value) { return new Holder<T>(value); }
    public T getValue() { return _value; }
    public void setValue(T value) { _value = value; }
}

...

Holder<String> x = Holder.of("123");
Holder<String> y = Holder.of("456");
someMethod(x, y);
return x.getValue() + y.getValue();

Some define a purpose-built type:

SomeMethodResult result = someMethod(x, y);
return result.getX() + result.getY();

Some would arrange for the work to be done inside the method, avoiding the need for by-reference arguments in the first place:

return someMethod(x, y);

Each of these techniques has advantages and disadvantages:

  • arrays: simple vs. ugly, relies on array having exactly one element
  • holder: safe vs. verbose, boxing
  • purpose-built type: safe vs. verbose, possible overkill
  • change method: safe, clean vs. not always possible

Personally, I think that Java messed up on this one. I'd rather avoid by-reference arguments, but I wish Java permitted multiple return values from a method. But, truthfully, I don't trip over this one very often. I wouldn't give a kidney for this feature. :)

半寸时光 2024-10-14 09:49:07

Java 不支持此功能。

Java does not support this feature.

謌踐踏愛綪 2024-10-14 09:49:07

Java 不支持通过引用传递基本类型。

int 包装为 Integer 不会有帮助,因为它是不可变类型(即一旦创建就无法更改)。

Java does not support passing primitive types by reference.

Wrapping an int as an Integer won't help, since it is an immutable type (i.e. cannot be changed once created).

对不⑦ 2024-10-14 09:49:07

Java没有这个内置的。 Integer 不行,因为它是不可变的。你无法改变它的状态。

您必须创建自己的可变包装类。但这是非常“危险”的,可能会导致意想不到的结果,所以尽量避免。

Java does not have this built-in. Integer won't do, because it is immutable. You can't change its state.

You will have to create your own mutable wrapper class. But this is very "dangerous" and may lead to unexpected results, so try to avoid it.

逆蝶 2024-10-14 09:49:07

传回值的唯一方法是传递对可变值的引用,

public void calc(int[] value) { value[0] = 1; }
public void calc(AtomicInteger value) { value.set(1); }

但是最简单的方法是返回所有更改的值

public int calc() { return 1; }

The only way to pass value back is to pass a reference to a mutable value like

public void calc(int[] value) { value[0] = 1; }
public void calc(AtomicInteger value) { value.set(1); }

however the simplest thing to do is to return all changed values

public int calc() { return 1; }
天涯沦落人 2024-10-14 09:49:07

Java 没有此功能

,这里是 Java 与 C# 比较的链接

http://www.harding.edu /fmccown/java_csharp_comparison.html

这就是为什么他们称 Java 为非纯 OOP 语言

Java dont have this feature

here is a link to Java vs C# comparison

http://www.harding.edu/fmccown/java_csharp_comparison.html

thats why they call Java a non pure OOP language

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