简体   繁体   English

为什么我不能传递与包裹/串行对象相对的引用地址?

[英]Why can I not pass reference address as opposed to Parcel/Serial Object?

According to the StackOverflow and the internet the way to pass an object is to either serialize or parcelize (or use "static" - frowned upon). 根据StackOverflow和Internet的说法,传递对象的方法是序列化或打包(或使用“静态”-皱眉)。

I would like to know why I can not pass the address 我想知道为什么我不能通过地址

     MainActivity.toString();  // MainActivity@b0ff5f80

Then while in the new class/Activity simply change the adress of the newly created object ? 然后在新类/活动中,只需更改新创建对象的地址?

MainActivity originalMainActivity = new MainActity();
originalMainActivity = getIntent.getStringExtra("originalMainActivity");
//yes I realize the above line is incompatible....Obj != String 

if the object has already been created and is sitting in the heap (I think ??) Why can we not reassign address ? 如果对象已经创建并且位于堆中(我认为是?),为什么不能重新分配地址? If we can can someone please provide an example ? 如果可以,请提供示例吗? Thank You. 谢谢。

ps (after all when we pass a class object in a Constructor are we not actually passing ref address) Thx again. ps(毕竟,当我们在构造函数中传递类对象时,我们实际上并没有传递参考地址)Thx。

You seem to be thinking in terms of addresses, as if you were in C or in C++. 您似乎在考虑地址,就像您使用C或C ++。

However: 然而:

  1. You are completely misunderstanding the purpose of toString : in the general case, the only valid use of toString is printing a representation of the object, for debugging purposes. 您完全误解了toString的目的:在一般情况下, toString的唯一有效用法是打印对象的表示形式,以用于调试目的。 There is nothing to say that that string can be used later to regenerate, or find the object again. 没有什么可说的,以后可以使用该字符串来重新生成或再次找到该对象。

  2. The Java language does not give any way to access memory addresses, in part because the garbage collector is generally free to rearrange memory. Java语言没有提供任何访问内存地址的方法,部分原因是垃圾回收器通常可以自由地重新排列内存。 The only way to work around that is to pin objects and operate outside of the Java language. 解决该问题的唯一方法是固定对象并在Java语言之外进行操作。

Besides, you should understand that when you get an object out of a Parcel , you are not necessarily in the same process (even if it is your process, your process may have been background-killed before being restarted) - so your method could only work in a restricted set of cases. 此外,您应该理解,当您从Parcel取出一个对象时,您不一定处于同一进程中(即使它是您的进程,您的进程也可能在重新启动之前已经被后台杀死)-因此您的方法只能在少数情况下工作。

Then while in the new class\\Activity simply change the adress of the newly created object ? 然后,在新类\\活动中,只需更改新创建对象的地址?

Among other reasons, because that is not possible in Java. 除其他原因外,因为在Java中这是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我应该创建一个新的对象引用来进行与使用现有对象相反的函数调用? - Why should I create a new object reference to do a function call opposed to using an existing one? 为什么 Arraylist 保存对象而不是引用变量,而不是 Array 保存引用变量或基元? - Why does Arraylist hold object rather than reference variable, as opposed to Array holding reference variable or primitives? 我该如何解析主机而非Android设备中的Parcel对象? - How can I parse Parcel object in host instead of android device? 如何在Java中传递Scala对象引用? - How can I pass a Scala object reference around in Java? 如何传递MyPojo对象作为对map方法的引用? - How can I pass the MyPojo object as reference to the map method? 创建对对象的引用时,为什么可以使用接口作为类型? - Why can I use an interface as a type when creating a reference to an object? 为什么我不能将方法引用直接分配给 Object 类型的变量? - Why can I not assign a method reference directly to a variable of Object type? 为什么我可以将原语传递给采用对象的方法? - Why can I pass a primitive to a method that takes an object? 如何将Long []写入包裹? - How can I write Long[] to a parcel? 如何传递对当前活动的引用 - How can I pass a reference to the current activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM