美文网首页
Parcelable 和 Serializable的区别和使用

Parcelable 和 Serializable的区别和使用

作者: 方_f666 | 来源:发表于2021-03-30 14:08 被阅读0次

实现方式:
Serializable:
public class UserBean implements Serializable {
private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}
Parcelable:
public class PriceBean implements Parcelable {
private String shopName;
private int price;

public PriceBean() {
}

protected PriceBean(Parcel in) {
    shopName = in.readString();
    price = in.readInt();
}

public static final Creator<PriceBean> CREATOR = new Creator<PriceBean>() {
    @Override
    public PriceBean createFromParcel(Parcel in) {
        return new PriceBean(in);
    }

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

public String getShopName() {
    return shopName;
}

public void setShopName(String shopName) {
    this.shopName = shopName;
}

public int getPrice() {
    return price;
}

public void setPrice(int price) {
    this.price = price;
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(shopName);
    dest.writeInt(price);
}

}

相关文章

网友评论

      本文标题:Parcelable 和 Serializable的区别和使用

      本文链接:https://www.haomeiwen.com/subject/bulwmctx.html