Tag: 分辨

Android – 多态和Parcelable

在从包裹中书写/阅读时,我无法弄清楚如何使用多态性。 我知道我需要在基类中实现Parcelable,并且还需要在所有派生类中实现(在子类具有我想要写入parcel的附加属性的情况下)。 我不明白和我不知道甚至可能的是 – 如何从Parcelable读取子类,即,我如何知道我从包裹中读取什么类的子类。 我可以做一些黑客,比如在包裹中写一些指示器,告诉我使用什么类加载器,但我认为会有一些更优雅的方式,否则没有多少使用多态。 为了说明我的问题,让我说我有这样的课程: Shape.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) […]