Serializable和Parcelable

1.Serializable

Serializable序列化的意思,表示将一个对象转换成可存储可传输状态。序列化后的对象可以在网络上进行传输,也可以存储至本地

public class Person implements Serializable {
    
    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;
    }
}
        Person person = new Person();
        person.setAge(25);
        person.setName("Yh");
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        intent.putExtra("person_data", person);
        startActivity(intent);
        
        Person person = (Person) getIntent().getSerializableExtra("person_data");

2.Parcelable

 不同于将对象进行序列化,Parcelable方式的实现原理是将一个完整的对象进行分解,而分解后的每一部分都是基础数据类型,这样也就实现传递对象的功能了。

public class Person implements Parcelable {

    private  String name;
    private int age;
    private Fruit fruit;

    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;
    }

    public Fruit getFruit() {
        return fruit;
    }

    public void setFruit(Fruit fruit) {
        this.fruit = fruit;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);
        dest.writeParcelable(fruit, flags);
    }

    public static final Person.Creator CREATOR = new Parcelable.Creator() {
        @Override
        public Person createFromParcel(Parcel source) {
            Person person = new Person();
            person.name = source.readString();
            person.age = source.readInt();
            person.fruit = source.readParcelable(Fruit.class.getClassLoader());
            return person;
        }

        @Override
        public Person[] newArray(int size) {
            return new Person[size];
        }
    };
}
public class Fruit implements Parcelable {
    private String name;

    public String getName() {
        return name;
    }

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

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

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

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        @Override
        public Fruit createFromParcel(Parcel source) {
            Fruit fruit = new Fruit();
            fruit.name = source.readString();
            return fruit;
        }

        @Override
        public Fruit[] newArray(int size) {
            return new Fruit[size];
        }
    };
}
Person person = new Person();
                Fruit fruit = new Fruit();
                fruit.setName("Apple");
                person.setName("Yh");
                person.setAge(25);
                person.setFruit(fruit);

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

 Person person = getIntent().getParcelableExtra("person_data");

你可能感兴趣的:(Serializable和Parcelable)