自定义对象流


自定义对象流


package com.atguigu.java;

import java.io.Serializable;

/**
 * Person需要满足如下的要求,方可序列化
 * 1.需要实现:Serializable接口(标识接口,不需要重写方法)
 * 2.当前类需要提供一个全局常量:serialVersionUID = ******L  提供的原因是:在修改类时,由于存在常量,序列化和反序列化不会出错
 * 3.除了当前Person类需要实现serializable接口之外,还必须保证其内部的所有属性:也必须是可序列化的
 *  (默认情况下,基本数据类型可序列化)
 *
 *
 * 补充:不能序列化static 和 Transient修饰的成员变量
 *
 * @author liangqichen
 * @create 2021-10-25 8:56
 */
public class Person implements Serializable {

    public static final long serialVersionUID = 42456132645L;

    private String name;
    private int age;
    private int id;
    private Account acct;

    public Person(String name, int age, int id, Account acct) {
        this.name = name;
        this.age = age;
        this.id = id;
        this.acct = acct;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", id=" + id +
                ", acct=" + acct +
                '}';
    }

    public Person(String name, int age, int id) {
        this.name = name;
        this.age = age;
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = 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;
    }

}

class Account implements Serializable {

    private double balance;
    public static final long serialVersionUID = 424562645L;

    public Account(double balance) {
        this.balance = balance;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    @Override
    public String toString() {
        return "Account{" +
                "balance=" + balance +
                '}';
    }
}

上一篇:POJ1837-Balance


下一篇:函数和递归