dfsljkjafdsk; 2022-05-25 09:43 采纳率: 100%
浏览 187
已结题

java oj,对象序列化

现有类Person和Book,其定义如下:

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

public class Person implements Serializable{

private static final long serialVersionUID = 1L;
private String name ;
private String gender ;
private LocalDate birthday ;
private String biography ;

public Person() {

}

public Person(String name , String gender , String biography ,
int year , int month ,int day) {
this.name = name ;
this.gender = gender ;
this.biography = biography ;
this.birthday = LocalDate.of(year , month , day) ;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public LocalDate getBirthday() {
return birthday;
}
public void setBirthday(LocalDate birthday) {
this.birthday = birthday;
}
public String getBiography() {
return biography;
}
public void setBiography(String biography) {
this.biography = biography;
}

@Override
public String toString() {
return "name: " + name + " , gender: " + gender + " , birthday: " 
+ birthday + " , biography: " + biography ;
}

}





import java.io.Serializable;

public class Book implements Serializable{

private static final long serialVersionUID = 1L;

private String name;
private Person author;
private int price;

public String getName() {
return name;
}

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

public Person getAuthor() {
return author;
}

public void setAuthor(Person author) {
this.author = author;
}

public int getPrice() {
return price;
}

public void setPrice(int price) {
this.price = price;
}

public Book() {

}

public Book(String name,Person author,int price) {
this.name = name;
this.author = author;
this.price = price;
}

@Override
public String toString() {
return "name: " + name + "\nauthor: " + author + "\nprice: " + price ;
}

}

有一段程序用objectOutputStream的writeobject()方法连续向文件dict.dic中写入了5个Book类型的对象。现请你写一段程序将这5个对象读出来。
注意:你的程序中要把Person和Book类的定义复制过去。
输入输出如下

img

  • 写回答

3条回答 默认 最新

  • CSDN专家-sinJack 2022-05-25 13:56
    关注
    import java.io.Serializable;
    import java.time.LocalDate;
    import java.io.*;
    import java.util.Scanner;
    public class Main{
        public static void main(String[] args) throws Exception{
            ObjectInputStream objectInputStream=new ObjectInputStream(new FileInputStream("dict.dic"));
            Scanner in=new Scanner(System.in);
            int num=in.nextInt();
            for(int i=1;i<num;i++){
                objectInputStream.readObject();
            }
            System.out.println(objectInputStream.readObject());
            objectInputStream.close();
        }
    }
    
    class Person implements Serializable{
    
        private static final long serialVersionUID = 1L;
        private String name ;
        private String gender ;
        private LocalDate birthday ;
        private String biography ;
    
        public Person() {
    
        }
    
        public Person(String name , String gender , String biography ,
                      int year , int month ,int day) {
            this.name = name ;
            this.gender = gender ;
            this.biography = biography ;
            this.birthday = LocalDate.of(year , month , day) ;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getGender() {
            return gender;
        }
        public void setGender(String gender) {
            this.gender = gender;
        }
        public LocalDate getBirthday() {
            return birthday;
        }
        public void setBirthday(LocalDate birthday) {
            this.birthday = birthday;
        }
        public String getBiography() {
            return biography;
        }
        public void setBiography(String biography) {
            this.biography = biography;
        }
    
        @Override
        public String toString() {
            return "name: " + name + " , gender: " + gender + " , birthday: "
                    + birthday + " , biography: " + biography ;
        }
    }
    
    class Book implements Serializable{
    
        private static final long serialVersionUID = 1L;
    
        private String name;
        private Person author;
        private int price;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Person getAuthor() {
            return author;
        }
    
        public void setAuthor(Person author) {
            this.author = author;
        }
    
        public int getPrice() {
            return price;
        }
    
        public void setPrice(int price) {
            this.price = price;
        }
    
        public Book() {
    
        }
    
        public Book(String name,Person author,int price) {
            this.name = name;
            this.author = author;
            this.price = price;
        }
    
        @Override
        public String toString() {
            return "name: " + name + "\nauthor: " + author + "\nprice: " + price ;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 6月2日
  • 已采纳回答 5月25日
  • 创建了问题 5月25日

悬赏问题

  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp