Parcelable 的 readInt()、readFloat() 等是否知道它们正在检索什么值?
Does the readInt(), readFloat() etc. of Parcelable know what value they are retrieving?
如果您的 class 实现了 parcelable,那么您知道系统需要一个构造函数来根据刚收到的包裹构造您的 class。
因此必须编写通常像下面这样的构造函数,这是让人有点困惑的地方:
public myClass(Parcel in){
this.type = in.readInt();
this.size = in.readInt();
}
你看,如果有参数,这些读取相关的方法是这样调用的:
this.type = in.readInt(type);
this.size = in.readInt(size);
然后很清楚发生了什么,但是它们根本没有任何参数。所以我想知道:这是应该如何完成的吗?这些方法 "smart" 是否以某种方式让您不必告诉他们要获取哪个字段并且他们总能设法获取正确的字段?
还是顺序的?如果我这样使用它:
this.size = in.readInt();
this.type = in.readInt();
我要得到的字段会不会全乱了?如果是这样的话,这不是在乞求错误吗?我可能因为一个粗心的错误而做错了很长时间,但过了一段时间才发现?
顺序很重要。所有写入必须以相同的顺序读取。
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?
确实
如果您的 class 实现了 parcelable,那么您知道系统需要一个构造函数来根据刚收到的包裹构造您的 class。
因此必须编写通常像下面这样的构造函数,这是让人有点困惑的地方:
public myClass(Parcel in){
this.type = in.readInt();
this.size = in.readInt();
}
你看,如果有参数,这些读取相关的方法是这样调用的:
this.type = in.readInt(type);
this.size = in.readInt(size);
然后很清楚发生了什么,但是它们根本没有任何参数。所以我想知道:这是应该如何完成的吗?这些方法 "smart" 是否以某种方式让您不必告诉他们要获取哪个字段并且他们总能设法获取正确的字段?
还是顺序的?如果我这样使用它:
this.size = in.readInt();
this.type = in.readInt();
我要得到的字段会不会全乱了?如果是这样的话,这不是在乞求错误吗?我可能因为一个粗心的错误而做错了很长时间,但过了一段时间才发现?
顺序很重要。所有写入必须以相同的顺序读取。
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?
确实