如果其中一个POJO字段是另一个POJO

时间:2015-08-05 15:37:07

标签: java serialization parcelable pojo

标题说明了一切,说我有这个阶级结构:

public class MyParentObject implements Comparable, Parcelable {
    private String param1, param2, param3, param4, param5;
    private Integer param6, param7;
    private double param8, param9;
    private InnerObject myChildObject = new InnerObject("test data");

//Other methods and constructors

}

如果有一个“不”可分辨的内部对象,我是否也必须实现它?

这是我的结构:

Class A
   - Class B
      - ArrayList<Class C>
      - List<Class D>

这是我到目前为止实施的内容:

A类:

 public A (Parcel in) {
    String[] data = new String[11];
    in.readStringArray(data);
    this.stringParam1 = data[0];
    this.stringParam2 = data[1];
    this.stringParam3 = data[2];
    this.stringParam4 = data[3];
    this.stringParam5 = data[4];
    this.integerParam1 = Integer.valueOf(data[5]);
    this.integerParam2 = Integer.valueOf(data[6]);
    this.doubleParam1 = Double.valueOf(data[7]);
    this.doubleParam2 = Double.valueOf(data[8]);
    this.booleanParam1 = Boolean.getBoolean(data[10]);

    this.classBParam = in.readParcelable(B.class.getClassLoader());
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(stringParam1);
    dest.writeString(stringParam2);
    dest.writeString(stringParam3);
    dest.writeString(stringParam4);
    dest.writeString(stringParam5);
    dest.writeString(Integer.toString(integerParam1));
    dest.writeString(Integer.toString(integerParam2));
    dest.writeString(Double.toString(doubleParam1));
    dest.writeString(Double.toString(doubleParam2));
    dest.writeString(Boolean.toString(booleanParam1));
    dest.writeParcelable(classBParam,flags);
}

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

    @Override
    public Object[] newArray(int size) {
        return new Object[0];
    }
};

0 个答案:

没有答案