Android修炼之道—Parcelable和Serializable

Intent中如何传递对象,一种是Bundle.putSerializable(Key,Object);另一种是Bundle.putParcelable(Key, Object);当然这些Object是有一定的条件的,前者是实现了Serializable接口,而后者是实现了Parcelable接口.

<span style="font-family:KaiTi_GB2312;font-size:18px;">//Person.java
import java.io.Serializable; 

public class Person implements Serializable { 
private static final long serialVersionUID = -7060210544600464481L;  
private String name;
private int age;  
public String getName(){
    return name;
}
public void setName(String name){
    this.name = name;
}
public int getAge(){
    return age;
}
public void setAge(int age){
    this.age = age;
}</span>

//Book.java
import android.os.Parcel;
impoet android.os.Parcelable;
public class Book implements Parcelable{
    private String bookName;
    private String author;
    private int publishTime;
    public String getBookName(){
        return bookname;
    }
        public void setBookName(String bookName) {  
        this.bookName = bookName;  
    }  
public String getAuthor() {  
        return author;  
    }  
public void setAuthor(String author) {  
        this.author = author;  
    }  
    public int getPublishTime() {  
        return publishTime;  
    }  
    public void setPublishTime(int publishTime) {  
        this.publishTime = publishTime;  
    }  
     
    public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>(){
      public Book createFromParcel source){
         Book mBook = new Book();
         mBook.bookName = source.readString();
         mBook.author = source.readString();  
         mBook.publishTime = source.readInt();  
        return mBook;  
      }
      public Book[] newArray(int size){
        return new Book[size];
      }
    };
    public int describeContents(){
       return 0;
    }
    public void writeToParcel(Parcel parcel, int flags){
       parcel.writeString(bookName);
       parcel.writeString(author);
       parcel.writeInt(publishTime);
    }
 }

你可能感兴趣的:(Android修炼之道—Parcelable和Serializable)