java对象序列化---serializable和Parcelable

serializable:




Parcelable:



public class ListInfo <span style="color:#ff0000;"><strong>implements Parcelable</strong></span> {
	String name;
	String time;
	int image;

	public ListInfo() {
		super();
	}

	public ListInfo(String name, String time, int image) {
		super();
		this.name = name;
		this.time = time;
		this.image = image;
	}

	public String getName() {
		return name;
	}

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

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public int getImage() {
		return image;
	}

	public void setImage(int image) {
		this.image = image;
	}

	<span style="color:#cc0000;"><strong>@Override
	public int describeContents() {
		return 0;
	}</strong></span>
<span style="background-color: rgb(192, 192, 192);">
	<span style="color:#ff0000;"><strong>/**
	 * 写入序列化,按顺序写入
	 */
	@Override
	public void writeToParcel(Parcel parcel, int arg1) {
		parcel.writeString(name);
		parcel.writeString(time);
		parcel.writeInt(image);
	}</strong></span></span><span style="color:#ff0000;"><strong>
</strong></span>
	<span style="color:#ff0000;"><strong>public static final Creator<ListInfo> CREATOR = new Creator<ListInfo>() {

		/**
		 * 从序列化通道中传出来
		 */
		@Override
		public ListInfo createFromParcel(Parcel parce) {
			//读取内容,加到info里面,相当于new一个info对象,所以直接写在构造器里面
			// String name=parce.readString();
			// String time=parce.readString();
			// int image=parce.readInt();
			// ListInfo info=new ListInfo();
			// info.setName(name);
			// info.setTime(time);
			// info.setImage(image);
			// return info;
			return new ListInfo(parce);
		}

		//序列化数组
		@Override
		public ListInfo[] newArray(int arg0) {
			return null;
		}

	};

	//有参构造器
	public ListInfo(Parcel parce) {
		name = parce.readString();
		time = parce.readString();
		image = parce.readInt();
	}</strong></span>

}


你可能感兴趣的:(java对象序列化---serializable和Parcelable)