Serializable序列化接口的使用

1.首先我们创建我们的类:

我们创建了一个User类,其中的一个字段是userName,另一个是一个Bean,注意:我们在使用的时候,必须保证没一个Bean对象都要实现 Serializable,不然会出现错误。

public class User implements Serializable{
    private String userName;
    private Bean bean;


    public static class Bean implements Serializable{
        private String longId;


        public String getLongId() {
            return longId;
        }


        public void setLongId(String longId) {
            this.longId = longId;
        }
    }
    
    public User(String userName,Bean bean) {
        this.userName = userName;
        this.bean = bean;
    }


    public Bean getBean() {
        return bean;
    }


    public void setBean(Bean bean) {
        this.bean = bean;
    }


    public String getUserName() {
        return userName;
    }


    public void setUserName(String userName) {
        this.userName = userName;
    }

}


2.使用起来十分简单:

(1)放置数据:

User.Bean bean = new User.Bean();
bean.setLongId("666666666");
User user = new User("Tom",bean);

(2)在intent传递数据的时候:

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

(3)在接收的地方:

Intent intent = getIntent();
User user = (User) intent.getSerializableExtra("user");

这样操作下来,我们的User就被传递过来了。在不同进程间,可以使用这种方式传递数据。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值