扩展实现Parcelable类 [英] Extending a class that implements Parcelable
问题描述
我有一个类,我们把它叫做A级,实现Parcelable。
我有第二类,我们将称之为B类,扩展A级。
我的问题是:
我如何写B类的成员变量的包裹,然后写它的父类的(即:A类的)?成员变量的包裹(以及随后,读取它们)
有一些漂亮的技巧,不需要重写类A的包裹code?或者,我只是需要重写包裹code类A和添加额外的code为B类的成员变量?
我如何写B类的成员变量的包裹,然后写它的父类的(即:A类的)成员变量的包裹
B类重写 writeToParcel()
从A类,链接到父,也加入了自己的对象的包裹
。
(以及随后,读取它们)?
B类器具公共静态最终Parcelable.Creator< MyParcelable> CREATOR
以这样一种方式,它可以让这两个类阅读他们的东西。如果你创建,需要一个 B类的构造函数的方式包裹
作为构造函数的参数,只是连锁超类的构造函数(让A类做的工作),然后阅读B类的数据。
关键是要以同样的顺序做他们两个。如果你打算让A类读取其数据的第一类必须先写它的数据。
有一些漂亮的技巧,不需要重写类A的包裹code?
继承和链接的超类。
I have a class, we'll call it class A, that implements Parcelable.
I have a second class, we'll call it class B, that extends class A.
My question is:
How do I write class B's member variables to the Parcel and then write it's parent class's (ie: class A's) member variables to the Parcel (and, subsequently, read them in)?
Is there some nifty trick to not needing to rewrite class A's Parcel code? Or do I just need to rewrite the Parcel code in class A and add additional code for class B's member variables?
How do I write class B's member variables to the Parcel and then write it's parent class's (ie: class A's) member variables to the Parcel
Class B overrides writeToParcel()
from Class A, chaining to the superclass and also adding its own objects to the Parcel
.
(and, subsequently, read them in)?
Class B implements public static final Parcelable.Creator<MyParcelable> CREATOR
in such a way that it can let both classes read their stuff in. If you take the approach of creating a constructor on Class B that takes a Parcel
as a constructor parameter, just chain to the superclass constructor (to let Class A do its work), then read Class B's data.
The key will be to do them both in the same order. If you intend to let Class A read its data first, Class A must write its data first.
Is there some nifty trick to not needing to rewrite class A's Parcel code?
Inheritance and chaining to the superclass.
这篇关于扩展实现Parcelable类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!