readObject()方法不是打印对象

时间:2015-08-15 13:42:36

标签: java serialization deserialization binaryfiles

在我必须做的作业中,我必须写一个学生对象,其中包括姓名,社会安全号码,通过的课程数量以及每门课程的成绩。我创建了一个gpa方法来计算gpa,并使用我自己的方法覆盖了writeObject和readObject方法。 Here是完整的源代码 - http://pastebin.com/dNMrc1zK

这是没有每个实例的getter和setter方法的样子:

import java.io.*;

public class Student implements Serializable{
        private String name;
        private int ssNum;
        private int coursesCompleted;
        private char[] grades;

        public Student(String n, int num, int c){
                n = name;
                num = ssNum;
                c = coursesCompleted;
        }

        public String getName() {
                return name;
        }

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

        public int getSsNum() {
                return ssNum;
        }

        public void setSsNum(int ssNum) {
                this.ssNum = ssNum;
        }

        public int getCoursesCompleted() {
                return coursesCompleted;
        }

        public void setCoursesCompleted(int coursesCompleted) {
                this.coursesCompleted = coursesCompleted;
        }

        public char[] getGrades() {
                return grades;
        }

        public void setGrades(char[] grades) {
                this.grades = grades;
        }
        public double gpa(char[] grades){

                double gpa = 0;

                for (int i = 0; i < grades.length; i++){
                        if (grades[i] == 'A'){
                                gpa += 4.0;
                        }
                        else if (grades[i] == 'B'){
                                gpa += 3.0;
                        }
                        else if (grades[i] == 'C'){
                                gpa += 2.0;
                        }
                        else if (grades[i] == 'D'){
                                gpa += 1.0;
                        }
                        else if (grades[i] == 'F'){
                                gpa += 0.0;
                        }
                }

                return gpa / grades.length;

        }

        public void ReadObject(ObjectInputStream in) throws IOException{
                String readTitle = "";

                for(int i = 0; i < name.length(); i++){
                        readTitle += in.readChar();
                }
                ssNum = in.readInt();
                coursesCompleted = in.readInt();

                for (int i = 0; i < grades.length; i++){
                        grades[i] = in.readChar();
                }
        }
        public void WriteObjectOverride(ObjectOutputStream out) throws IOException, ClassNotFoundException{

                out.writeChars(name);
                out.writeInt(ssNum);
                out.writeInt(coursesCompleted);

                for (int i = 0; i < grades.length; i++){
                        out.writeChar(grades[i]);
                }
                gpa(grades);
        }
        public String toString(){
                   String student = name + " " +  "\nGPA: " + gpa(grades);
                   return student;
                }
}


public void ReadObject(ObjectInputStream in) throws IOException{
    String readTitle = "";

    for(int i = 0; i < name.length(); i++){
        readTitle += in.readChar();
    }
    ssNum = in.readInt();
    coursesCompleted = in.readInt();

    for (int i = 0; i < grades.length; i++){
        grades[i] = in.readChar();
    }
}
public void WriteObjectOverride(ObjectOutputStream out) throws IOException, ClassNotFoundException{

    out.writeChars(name);
    out.writeInt(ssNum);
    out.writeInt(coursesCompleted);

    for (int i = 0; i < grades.length; i++){
        out.writeChar(grades[i]);
    }
    gpa(grades);
}
public String toString(){
       String student = name + " " +  "\nGPA: " + gpa(grades);
       return student;
    }
}

然后我必须编写一个单独的程序,将对象写入文件。我创建了一个构造函数,以便它给出名称,ss编号和课程完成的值,然后我使用setter方法设置grade数组值。然后我使用了我必须使用writeObject的值并编写了这样的对象:

            Student john = new Student("John Doe", 123456, 4);
    Student jane = new Student("Jane Doe", 987654, 3);
    Student jack = new Student("Jack Doe", 528491, 4);

    ObjectOutputStream out;

    john.setGrades(student1);
    jane.setGrades(student2);
    jack.setGrades(student3);

    try {
        out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("students.bin")));

        out.writeObject(john);
        out.writeObject(jane);
        out.writeObject(jack);

        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

我遇到的问题是当我使用另一个程序来执行readObject()方法时,如下所示:

    public static void main (String[] args)throws FileNotFoundException, IOException, ClassNotFoundException{
    Student student = null;
    try(ObjectInputStream in = new ObjectInputStream(new FileInputStream("students.bin"));){

        student = (Student)in.readObject();
        System.out.println(student);
    }

它打印的行不是我预期的行:

UnitSeven.Student@70dea4e

有谁知道如何解决这个问题,以便它显示实际的对象?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

执行时:

System.out.println(student);

实际发生的是调用学生对象的toString()方法。由于您没有覆盖学生类的toString()方法,因此您运行的是Object类,它是java中toString()方法中所有类的父类。此Object类的toString()返回学生的类名和哈希码。 Hashcode是你看到的mumbo jumbo号码。这个哈希码是java中的标准标识符。

您应该在toString()类中覆盖Object类的Student方法,以便打印所需的输出。