Java Android 中的 Parcelable 和继承

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4049627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:56:50  来源:igfitidea点击:

Parcelable and inheritance in Android

javaandroiddesign-patternsinheritanceparcelable

提问by Vincent Mimoun-Prat

I got an implementation of Parcelable working for a single class that involves no inheritance. I have problems figuring out the best way to implement the interface when it come to inheritance. Let's say I got this :

我得到了一个 Parcelable 的实现,它为一个不涉及继承的类工作。当涉及到继承时,我在找出实现接口的最佳方法时遇到了问题。假设我得到了这个:

public abstract class A {
    private int a;
    protected A(int a) { this.a = a; }
}

public class B extends A {
    private int b;
    public B(int a, int b) { super(a); this.b = b; }
}

Question is, which is the recommended way to implement the Parcelable interface for B (in A? in both of them? How?)

问题是,这是为 B 实现 Parcelable 接口的推荐方法(在 A 中?在它们两个中?如何?)

采纳答案by Vincent Mimoun-Prat

Here is my best solution, I would be happy to hear from somebody that had a thought about it.

这是我最好的解决方案,我很高兴听到有人对此有想法。

public abstract class A implements Parcelable {
    private int a;

    protected A(int a) {
        this.a = a;
    }

    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(a);
    }

    protected A(Parcel in) {
        a = in.readInt();
    }
}

public class B extends A {
    private int b;

    public B(int a, int b) {
        super(a);
        this.b = b;
    }

    public static final Parcelable.Creator<B> CREATOR = new Parcelable.Creator<B>() {
        public B createFromParcel(Parcel in) {
            return new B(in);
        }

        public B[] newArray(int size) {
            return new B[size];
        }
    };

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel out, int flags) {
        super.writeToParcel(out, flags);
        out.writeInt(b);
    }

    private B(Parcel in) {
        super(in);
        b = in.readInt();
    }
}

回答by Alaa Awad

Here is the implementation for class A in a real world setting since class B will likely have more than one object with different types other than int

这是在现实世界设置中类 A 的实现,因为类 B 可能有多个不同类型的对象,而不是 int

It uses reflection to get the types. Then uses a sorting function to sort the fields so that reading and writing happen in the same order.

它使用反射来获取类型。然后使用排序函数对字段进行排序,以便读取和写入以相同的顺序进行。

https://github.com/awadalaa/Android-Global-Parcelable

https://github.com/awadalaa/Android-Global-Parcelable

回答by osxdirk

This is my variant. I think it's nice because it shows the symmetry between the virtual read- and write- methods very clearly.

这是我的变种。我认为这很好,因为它非常清楚地显示了虚拟读写方法之间的对称性。

Side note: I think Google did a really poor job at designing the Parcelable interface.

旁注:我认为谷歌在设计 Parcelable 界面方面做得非常糟糕。

public abstract class A implements Parcelable {
    private int a;

    protected A(int a) {
        this.a = a;
    }

    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(a);
    }

    public void readFromParcel(Parcel in) {
        a = in.readInt();
    }
}

public class B extends A {
    private int b;

    public B(int a, int b) {
        super(a);
        this.b = b;
    }

    public static final Parcelable.Creator<B> CREATOR = new Parcelable.Creator<B>() {
        public B createFromParcel(Parcel in) {
            B b = new B();
            b(in);
            return b;
        }

        public B[] newArray(int size) {
            return new B[size];
        }
    };

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel out, int flags) {
        super.writeToParcel(out, flags);
        out.writeInt(b);
    }

    public void readFromParcel(Parcel in) {
        super(in);
        b = in.readInt();
    }
}