Android中两种序列化方式的比较Serializable和Parcelable

版权声明:本文为博主原创文章,欢迎转载但需注明出处谢谢! https://blog.csdn.net/dongxianfei/article/details/84107667

Serializable和Parcelable接口可以完成对象的序列化过程,当我们需要通过Intent和Binder传输数据时就需要使用这两种序列化方式。还有,我们需要对象持久化到存储设备或者通过网络传输给其他客户端,这个使用也需要使用Serializale来完成对象的序列化。

Serializable接口

public class User1 implements Serializable{
    private static final long serialVersionUID = 1L;

    public int userId;
    public String userName;
    public String password;

    @Override
    public String toString() {
        return "User1{" +
                "userId=" + userId +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

private void startUser1(){
        User1 user1 = new User1();
        user1.userId = 1;
        user1.userName = "name";
        user1.password = "pwd";

        Intent intent = new Intent(MainActivity.this,SecondActivity.class);
        intent.putExtra("user1",user1);
        startActivity(intent);
    }

 private void getUser1(){
        User1 user1 = (User1) getIntent().getSerializableExtra("user1");
        Log.e("User1",user1.toString());
    }
//结果
User1{userId=1, userName='name', password='pwd'}

另外一种序列化到本地

//序列化到本地
User1 user1=new User(1,"name","pwd");
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("user1.obj"));
out.writeObject(user1);
out.close;

//反序列化
ObjectInputStream in=new ObjectInputStream(new FileInputStream("user1.obj"));
User1 user1=(User1)in.readObject();
in.close();

Parcelable接口
Parcelable接口是Android SDK提供的一种专门用于Android应用中对象的序列化和反序列化的方式,相比于Seriablizable具有更好的性能。实现Parcelable接口的对象就可以实现序列化并可以通过Intent和Binder传递。

public class Book implements Parcelable{
    public int num;

    public Book(int num){
        this.num = num;
    }

    protected Book(Parcel in) {
        this.num = in.readInt();
    }

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

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

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

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

    @Override
    public String toString() {
        return "Book{" +
                "num=" + num +
                '}';
    }
}
public class User2 implements Parcelable{

    public int userId;
    public String userName;
    public String password;
    public Book book;

    public User2(int userId,String userName,String password,Book book){
        this.userId = userId;
        this.userName = userName;
        this.password = password;
        this.book = book;
    }

    protected User2(Parcel in) {
        this.userId = in.readInt();
        this.userName = in.readString();
        this.password = in.readString();
        this.book = in.readParcelable(Thread.currentThread().getContextClassLoader());
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.userId);
        dest.writeString(this.userName);
        dest.writeString(this.password);
        dest.writeParcelable(this.book,0);
    }

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

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

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

    @Override
    public String toString() {
        return "User2{" +
                "userId=" + userId +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", book=" + book +
                '}';
    }
}
private void startUser2() {
        Book book = new Book(3);
        User2 user2 = new User2(1,"name","pwd",book);
        Intent intent = new Intent(MainActivity.this,SecondActivity.class);
        intent.putExtra("user2",user2);
        startActivity(intent);
    }

private void getUser2(){
        User2 user2 = (User2) getIntent().getParcelableExtra("user2");
        Log.e("User2",user2.toString());
    }
//结果
User2{userId=1, userName='name', password='pwd', book=Book{num=3}}

注意

  • 静态成员变量属于类不属于对象,所以不参与序列化过程;
  • 用transient关键字标记的成员变量不参与序列化过程;
    序列化方法注解
    二者却别

猜你喜欢

转载自blog.csdn.net/dongxianfei/article/details/84107667