我不知道在从包裹中写入/读取时如何使用多态性。 我知道我需要在基类和所有派生类中实现 Parcelable(如果子类具有我想写入 parcel 的附加属性)。 我不明白的是,我不知道它是否可能是——如何从 Parcelable 中读取子类,即我如何知道我正在从包裹中读取哪种子类。 我可以做一些 hack,比如在 parcel 中写入一些指示器,告诉我使用什么类加载器,但我认为会有一些更优雅的方法,否则多态性就没有太多用处了。
为了说明我的问题,假设我有这样的类(class):
形状类
public class Shape implements Parcelable {
public float area;
public Shape() {
}
public Shape(Parcel in) {
area = in.readFloat();
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeFloat(area);
}
//CREATOR etc..
}
RectangleShape.class
public class RectangleShape extends Shape {
float a;
float b;
public RectangleShape(Parcel in) {
super(in);
a = in.readFloat();
b = in.readFloat();
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeFloat(area);
dest.writeFloat(a);
dest.writeFloat(b);
}
//CREATOR etc..
}
CircleShape.class
public static class CircleShape extends Shape {
float r;
public CircleShape(Parcel in) {
super(in);
r = in.readFloat();
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeFloat(area);
dest.writeFloat(r);
}
//CREATOR etc..
}
现在,在其他一些类中,我有这样的东西:
public class Geometry implements Parcelable {
Shape myShape;
public Geometry (boolean condition) {
if (condition)
myShape = new CircleShape(4.5);
else
myShape = new RectangleShape(3.4, 3.5);
}
@Override
public Geometry(Parcel in) {
in.readParcelable(??? - **how do I know what class loader to put here?** )
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(myShape,flags);
}
//CREATOR etc..
}
有什么方法可以在写入 parcel 时“告诉”android 它是什么子类,而无需隐式检查实例类型?
最佳答案
我写了这个小 fragment 并将其加载到主要 Activity 中的 FrameLayout
中进行检查 - 我得到了肯定的结果(从 parcel 重建的对象是 DerivedClass
虽然对象的引用都是 ObjectClass
类型)。
import android.app.Fragment;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Fragment2 extends Fragment {
public ObjectClass obj;
public static final String OBJECT_KEY = "Object key";
public static Fragment2 newInstance(){
Fragment2 fragment = new Fragment2();
Bundle args = new Bundle();
ObjectClass obj = new DerivedClass(); //a DerivedClass object
//referenced by an ObjectClass reference
args.putParcelable(OBJECT_KEY, obj);
fragment.setArguments(args);
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main2, container, false);
if (savedInstanceState == null){
Bundle args = getArguments();
obj = args.getParcelable(OBJECT_KEY); //without supplying the ClassLoader
((TextView)view.findViewById(R.id.section_label)).setText(obj.getTitle());
//The text that is displayed is: "Child class"!
}
return view;
}
//The parent class
public static class ObjectClass implements Parcelable {
String title = "Parent class";
public String getTitle(){
return title;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
}
public static final Creator<ObjectClass> CREATOR = new Creator<ObjectClass>() {
@Override
public ObjectClass createFromParcel(Parcel source) {
return new ObjectClass();
}
@Override
public ObjectClass[] newArray(int size) {
return new ObjectClass[size];
}
};
}
//The child class
public static class DerivedClass extends ObjectClass {
String title2 = "Child class";
public String getTitle() {
return title2;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
}
public static final Parcelable.Creator<DerivedClass> CREATOR = new Parcelable.Creator<DerivedClass>() {
@Override
public DerivedClass createFromParcel(Parcel source) {
return new DerivedClass();
}
@Override
public DerivedClass[] newArray(int size) {
return new DerivedClass[size];
}
};
}
}
在主要 Activity 中,在 onCreate(Bundle)
方法中:
getFragmentManager().beginTransaction().replace(R.id.frameContainer, Fragment2.newInstance()).commit();
导致我进行此检查的想法是:由于 android 必须弄清楚从 parcel 中重新创建对象时要使用哪个类(要使用哪个 CREATOR
),我认为在将其放入包中时,它必须将此附加信息与指定信息(在 writeToParcel(Parcel, int)
方法中指定)一起存储。为了确定这个类,我认为它可以使用引用的类型或使用对象本身(例如,在其上使用 getClass()
)。如果它使用对象类型本身,那当然意味着多态性是可能的。从我上面的检查来看,情况确实如此。
您的示例的结论和猜测:
我认为对于您的示例,请尽量不要显式传递 ClassLoader。传递 null
。来自 Parcel#readParcelable(ClassLoader)
参数的文档:
ClassLoader: A ClassLoader from which to instantiate the Parcelable object, or null for the default class loader.
我没有尝试使用您的配置,所以我不确定它是否会像我的示例中那样工作。具体来说,我不确定 Parcel#readParcelable(ClassLoader=null)
是否真的以与 Bundle#getParcelable(String)
相同的方式工作。但如果我的理由是正确的 - 它应该(我假设 Bundle#getParcelable(String)
使用默认的 ClassLoader 只要 Bundle#setClassLoader(ClassLoader)
没有被调用) .
请发表评论,让我知道它是否对您有用。
关于java - Android - 多态性和Parcelable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39935716/