美文网首页后端小树林
使用java对与具有共享对象的数据进行序列化

使用java对与具有共享对象的数据进行序列化

作者: 奔跑的蛙牛 | 来源:发表于2018-08-11 17:36 被阅读0次

    objectStream.java

    package randomAccess;
    
    import Employee.Employee;
    import Employee.Manager;
    
    import java.io.*;
    
    
    public class objectStream {
        public static void main(String[] args) throws ClassNotFoundException{
            Employee harry = new Employee("harry",50000d,1989,10,1);
            Manager carl = new Manager("carl",8000d,1983,12,22);
            carl.setSecretary(harry);
            Manager tony = new Manager("tony",4000d,1990,3,15);
            tony.setSecretary(harry);
            Employee[] staff = new Employee[3];
            staff[0] = carl;
            staff[1] = harry;
            staff[2] = tony;
            try(ObjectOutputStream out = new ObjectOutputStream((new FileOutputStream("employee.dat")))){
                out.writeObject(staff);
            }catch (IOException e){
                System.out.println(e);
            }
            try(ObjectInputStream in = new ObjectInputStream(new FileInputStream("employee.dat"))){
    
                Employee[] newStaff = (Employee[])in.readObject();
    
                //raise secretary's salary
    
                newStaff[1].raiseSalaryt(10);
                for (Employee e:newStaff
                     ) {
                    System.out.println(e);
                }
            }catch (IOException e){
    
            }
        }
    }
    ##  Employee
    

    package Employee;

    import java.io.Serializable;
    import java.time.LocalDate;

    public class Employee implements Serializable{
    private String name;
    private double salary;
    private LocalDate hireDay;
    public Employee(String name,Double salary,int year,int month, int day ){
    this.name = name;
    this.salary = salary;
    this.hireDay = LocalDate.of(year,month,day);
    }

    public String getName() {
        return name;
    }
    
    public double getSalary() {
        return salary;
    }
    
    public LocalDate getHireDay() {
        return hireDay;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public void setSalary(double salary) {
        this.salary = salary;
    }
    
    public void setHireDay(LocalDate hireDay) {
        this.hireDay = hireDay;
    }
    public void raiseSalaryt(double byPercent){
        double raise = salary*byPercent/100;
        salary+=raise;
    }
    
    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", salary=" + salary +
                ", hireDay=" + hireDay +
                '}';
    }
    

    }

    Manager

    package Employee;
    
    import java.io.Serializable;
    
    public class Manager extends Employee implements Serializable{
        private double bouns;
        private Employee secretary;
    
        public void setSecretary(Employee secretary) {
            this.secretary = secretary;
        }
    
        public Manager(String name, double salary, int year, int month, int day) {
            super(name,salary,year,month,day);
            bouns = 0;
        }
        public double getSalary(){
            double baseSalary = super.getSalary();
            return baseSalary+bouns;
        }
        public void setBouns(double d){
            bouns = d;
        }
    }
    

    运行结果截图

    image.png

    相关文章

      网友评论

        本文标题:使用java对与具有共享对象的数据进行序列化

        本文链接:https://www.haomeiwen.com/subject/xsvdbftx.html