Java - 将指向对象的指针传递给函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22076426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - pass a pointer to an object to a function
提问by Christian Schnorr
What I want to achieve is something like this:
我想要实现的是这样的:
MyClass object = null;
doStuff(&object);
// `object` can now be non-null
What I'm currently doing is this, but I think there must be a better way to achieve this behavior in Java:
我目前正在做的是这个,但我认为必须有更好的方法来在 Java 中实现这种行为:
MyClassPointer pointer = new MyClassPointer(null);
// pointer.object is null
doStuff(pointer);
// pointer.object can now be non-null
采纳答案by ajb
If you really want doStuff
to return two values: There may well be a better way to design this. Nevertheless, if you've decided that having doStuff
return two values is really the best design, it can be done with a simple helper class:
如果你真的想doStuff
返回两个值:很可能有更好的设计方法。尽管如此,如果您已经确定doStuff
返回两个值确实是最好的设计,那么可以使用一个简单的辅助类来完成:
static class MyClassAndBoolean {
public MyClass obj;
public boolean b;
public MyClassAndBoolean(MyClass obj, boolean b) { this.obj = obj; this.b = b; }
}
and then change doStuff
's return type to MyClassAndBoolean
. This is one of the very few cases where I think public fields in a class are OK. Since you're defining a simple class just to use as a function result, rather than representing a coherent concept, the usual concerns about defining accessors instead of exposing fields, etc., don't really apply. (P.S. I just guessed at boolean
for the other type, but it can be anything, of course.)
然后将doStuff
的返回类型更改为MyClassAndBoolean
. 这是我认为类中的公共字段可以的极少数情况之一。由于您定义一个简单的类只是为了用作函数结果,而不是表示一个连贯的概念,因此通常担心定义访问器而不是公开字段等,实际上并不适用。(PS 我只是猜测boolean
其他类型,但它可以是任何东西,当然。)
Another workaround:
另一种解决方法:
MyClass[] obj = new MyClass[1];
result = doStuff(obj);
Change doStuff
's parameter type to MyClass[]
, and have it stuff the new object into parameter[0]
. I do see this idiom used in some of the Java and Android library methods.
将doStuff
的参数类型更改为MyClass[]
,并将新对象填充到 中parameter[0]
。我确实在一些 Java 和 Android 库方法中看到了这个习语。
回答by Brian Agnew
Why not simply:
为什么不简单:
MyClass object = doStuff();
which is muchmore intuitive IMHO. Otherwise you have to pass a reference (not pointer!) to a container object to your method, as you've identified. That's not a common pattern in the Java world.
恕我直言,这更直观。否则,您必须将容器对象的引用(而不是指针!)传递给您的方法,正如您所确定的那样。这不是 Java 世界中的常见模式。
回答by RMachnik
As far as I know java language it is the only way to do that. But you can simply implement method into your class. And in your method also there is possibility to return your object.
据我所知,Java 语言是唯一的方法。但是你可以简单地在你的类中实现方法。在你的方法中也有可能返回你的对象。
public MyObject myFunction(){
//do stufff...
return new MyObject();
}
MybObject object = myFucntion();
Or you can do it in your way. Also using Reflection you can invoke your method.
或者你可以按照你的方式来做。同样使用反射,您可以调用您的方法。
Method method = foo.getClass().getMethod("doSomething", null);
method.invoke(foo, null);
回答by Thomas
No, there is no better way since in Java you don't have pointers the way you have them in C++.
不,没有更好的方法,因为在 Java 中您没有像在 C++ 中那样拥有指针的方式。
In Java references (much like pointers in C++) can't be directly accessed and are always passed by value. Thus you can't change the value of a reference within a method.
在 Java 中引用(很像 C++ 中的指针)不能直接访问并且总是按值传递。因此,您不能在方法中更改引用的值。
回答by robermann
Use the Visitor pattern, your MyClass being what have to be 'visited' by dostuff
使用访问者模式,您的 MyClass 是 dostuff 必须“访问”的内容