简体   繁体   English

Parcelable的readInt(),readFloat()等是否知道它们正在检索什么值?

[英]Does the readInt(), readFloat() etc. of Parcelable know what value they are retrieving?

If your class implement parcelable then you know that you need a constructor for the system to construct your class based on the parcel it just received. 如果您的类实现了可打包的,那么您就知道系统需要一个构造函数来根据刚刚收到的小包来构造您的类。

So a constructor usually like the one below will have to be written and this is where it gets a bit confusing: 因此,必须编写通常类似于以下构造函数的构造函数,这会使它变得有些混乱:

public myClass(Parcel in){
   this.type = in.readInt();
   this.size = in.readInt();
}

You see, if there is a parameter and these read related methods are called like this: 您会看到是否有一个参数,并且这些与读取相关的方法的调用方式如下:

   this.type = in.readInt(type);
   this.size = in.readInt(size);

Then it's all quite clear what's going on but instead, they don't have any parameters at all. 然后就很清楚发生了什么,但是它们根本没有任何参数。 So I'm wondering: is this how it is should be done? 所以我想知道:这应该怎么做? Are these method "smart" in some way that you don't have to tell them what field to get and they will always manage to get the right one? 这些方法是否在某种程度上是“智能”的,您不必告诉他们要获得哪个领域,他们将始终设法找到合适的领域?

Or is it sequential? 还是顺序的? If I use it like this: 如果我这样使用它:

   this.size = in.readInt();
   this.type = in.readInt();

The fields I'm about to get will all get messed up? 我要去的田地都弄乱了吗? If that's the case isn't this begging for bugs? 如果是这种情况,这不是在乞求错误吗? I could be doing the wrong thing for a long time due to a careless mistake yet only to find it quite some time after? 由于粗心大意的错误,我可能会做错很长时间的事情,但过了一段时间才发现?

The order does matter. 顺序很重要。 All writes must be read in the same order. 必须以相同的顺序读取所有写入。

If that's the case isn't this begging for bugs? 如果是这种情况,这不是在乞求错误吗? I could be doing the wrong thing for a long time due to a careless mistake yet only to find it quite some time after? 由于粗心大意的错误,我可能会做错很长时间的事情,但过了一段时间才发现?

Indeed 确实

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM