java(初学者):返回一个对象?它是作为常量引用返回还是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4205362/
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 (beginner) : returning an object ? is it returned as a constant reference or what?
提问by Ismail Marmoush
I have a function that returns a user-defined object. First I want to know if that object is returned by reference and what if it was private?
我有一个返回用户定义对象的函数。首先我想知道该对象是否通过引用返回,如果它是私有的呢?
Also, how do I return it as Constant (final) reference because I don't want someone to mess with it? I'm so confused between returning an object and returning object.copy(); or object.clone();
另外,我如何将它作为常量(最终)引用返回,因为我不希望有人弄乱它?我在返回一个对象和返回 object.copy() 之间很困惑;或 object.clone();
采纳答案by Pablo Santa Cruz
In Java, You always return a reference (unless returned value is a primitive type such as int
, float
, char
, ...).
在 Java 中,您总是返回一个引用(除非返回的值是原始类型,例如int
, float
, char
, ...)。
So, if you don't want the returned object to be modified, you mustreturn a full copy of it (you could use Clonable
interface and clone
method if your class defines it).
因此,如果您不想修改返回的对象,则必须返回它的完整副本(如果您的类定义了Clonable
接口和clone
方法,则可以使用它)。
回答by GuruKulki
It returns the object's reference.
它返回对象的引用。
say suppose you have a method call like.
假设您有一个方法调用,例如。
Object obj = makeObject();
which creates an Object and returns(which is the reference of the object created in the makeObject method).
它创建一个对象并返回(这是在 makeObject 方法中创建的对象的引用)。
回答by Jeremy
1) Yes, it returns a reference to the object.
1) 是的,它返回对对象的引用。
2) If the method is private, then it can only be called from within the class itself.
2) 如果该方法是私有的,则只能从类本身内部调用它。
3) Making an object final
does not stop others from calling methods on it. There are ways to make an object immutable by using packages and method visibility. (Look up how public, protected, and private work)
3)创建一个对象final
并不会阻止其他人调用它的方法。有一些方法可以通过使用包和方法可见性使对象不可变。(查看公共、受保护和私人的工作方式)
4) There is no Object.copy()
4)没有 Object.copy()
5) Object.clone()
is a messy beast.
5)Object.clone()
是一头凌乱的野兽。
回答by Ferdi265
So, to answer your questions you have to at first know how Java passes Variables.
因此,要回答您的问题,您首先必须了解 Java 如何传递变量。
a Variable has a value:
一个变量有一个值:
int i = 1234;
Person p = new Person("Peter");
Now, the Variable i contains exactly 1234, while the Variable p contains the Memory Adress of the created Person.
现在,变量 i 包含正好 1234,而变量 p 包含创建的 Person 的内存地址。
so i contains 1234 and p contains the adress (let's say a4dfi3).
所以我包含 1234 和 p 包含地址(假设 a4dfi3)。
anyMethodYouLike(p);
System.out.println(p.getName());
public void anyMethodYouLike(Person somePerson) {
somePerson.rename("Homer");
}
so in this example, we give the Method anyMethodYouLike the Variable p... wait! we give the Method the value of the Variable (a4dfi3). The Method then calls rename on this Variable (which still has the same adress as p has, hence it modifies the same Person that p points to). So, after the Method, the Name of the Person p points to, gets printed, which results in "Homer".
所以在这个例子中,我们给方法 anyMethodYouLike 变量 p...等等!我们给方法变量的值 (a4dfi3)。该方法然后对该变量调用重命名(它仍然具有与 p 相同的地址,因此它修改了 p 指向的同一个 Person )。因此,在方法之后,p 指向的人的姓名被打印出来,结果是“荷马”。
someOtherMethod(p);
System.out.println(p.getName());
public void someOtherMethod(Person somePerson) {
somePerson = new Person("Walter");
}
In THIS example we still give the adress of our Person called "Peter" to the Method. But this time, the Method creates a new Person in somePerson (therefore overriding the adress in somePerson to.. let's say 13n37s. BUT! the Person at a4dfi3 wasn't changed! The print call still outputs "Peter" and not "Walter".
在这个例子中,我们仍然将名为“Peter”的人的地址提供给方法。但是这一次,该方法在 somePerson 中创建了一个新的 Person(因此将 somePerson 中的地址覆盖为 .. 比方说 13n37s。但是!a4dfi3 处的 Person 没有改变!打印调用仍然输出“Peter”而不是“Walter” .
Now, let's see how this behaves with primitives:
现在,让我们看看它如何处理原语:
someMethod(i);
System.out.println(i);
public void someMethod(int someInt) {
someInt++;
}
So, the Value of i (1234) gets passed to someInteger. Then someInteger gets incremented to 1235. But i is still 1234.
因此,i (1234) 的值被传递给 someInteger。然后 someInteger 增加到 1235。但 i 仍然是 1234。
This is the big difference between Objects and primitives in Java.
这是 Java 中对象和原语之间的巨大区别。
Now to your questions: 1. As you can read here, yes Java always passes the Reference Adress of the Object. 2. If you don't want someone to mess with the values of your Objects, you HAVE to first create a new Object with that information (e.g with Cloneable and clone()), but it's a real mess because you have to make sure, that everything in your Object that is not primitive gets re-created, which is just awful when you have huge Tree-structures of Objects.
现在回答您的问题: 1. 正如您在此处阅读的那样,是的,Java 总是传递对象的引用地址。2. 如果你不想让别人弄乱你对象的值,你必须先用这些信息创建一个新对象(例如,使用 Cloneable 和 clone()),但这真的很麻烦,因为你必须确保,你的对象中所有非原始的东西都会被重新创建,当你有巨大的对象树结构时,这太可怕了。
I hope I could help, Ferdi265
我希望我能帮上忙,Ferdi265
回答by newacct
What you should really realize is that there is nothing special about "passing" or "returning". Whenever you "pass" or "return" something, it just passes the value of the thing. Period. For any type. Whenever you "pass" or "return" something, it's exactly the same as simply assigning it to a variable.
您真正应该意识到的是,“通过”或“返回”并没有什么特别之处。每当您“传递”或“返回”某物时,它只会传递该事物的值。时期。对于任何类型。每当您“传递”或“返回”某些东西时,它与简单地将其分配给变量完全相同。
However, what isthe value of the thing you are passing or returning? That is where your confusion seems to lie. Your question asks about "returning an object"; however, such a thing does not make sense in Java. Objects are not values in Java. It is impossible to have a variable whose value is an object.
然而,什么是你正在传递或退回的物品的价值?这就是你的困惑似乎所在的地方。你的问题是关于“返回一个对象”;然而,这样的事情在 Java 中没有意义。对象不是 Java 中的值。不可能有一个值为对象的变量。
The only types in Java are primitive types and reference types. Hence the only values are primitives and references. (References are pointers to objects.) In Java, we only manipulate objects through references (pointers to objects). We cannot store an object in a variable; but we can store a reference (pointer to an object) in a variable. So when you talk about passing or returning objects, you are almost certainly instead talking about passing or returning references. And, as said before, there is nothing special about passing or returning references -- the value of the reference (a pointer) is passed or returned.
Java 中唯一的类型是原始类型和引用类型。因此唯一的值是基元和引用。(引用是指向对象的指针。)在 Java 中,我们只通过引用(指向对象的指针)来操作对象。我们不能将对象存储在变量中;但是我们可以在变量中存储引用(指向对象的指针)。所以当你谈论传递或返回对象时,你几乎肯定是在谈论传递或返回引用。而且,如前所述,传递或返回引用并没有什么特别之处——传递或返回引用的值(指针)。