在具体类中,我的ObjectOutputStream无法正常工作,无法显示正确的结果

我的用户摘要类

package mainpkg;

abstract public class User {
    public int id;            
    public String name,email,mobile;     

    public User() {
    }

    public User(int id,String name,String email,String mobile) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.mobile = mobile;
    }

    abstract public void setInfo();       
    abstract public void showInfo(); 
}

这是我的学生创造课程

package mainpkg;

import java.io.Serializable;
import java.util.Scanner;

public class Student extends User implements Serializable  {
    private float cgpa;
    private String dept;


    public Student(float cgpa,String dept,int id,String mobile) {
        super(id,name,mobile);
        this.cgpa = cgpa;
        this.dept = dept;
    }

    @Override
    public void setInfo() {
        System.out.println("Set Student Info");
        Scanner s = new Scanner(System.in);

        System.out.print("Enter ID: "); id = s.nextInt();
        s.nextLine();
        System.out.print("Enter Name: "); name = s.nextLine();
        System.out.print("Enter Email: "); email = s.nextLine();
        System.out.print("Enter Mobile: "); mobile = s.nextLine();
        System.out.print("Enter dept: "); dept = s.nextLine();
        System.out.print("Enter CGPA: "); cgpa = s.nextFloat();
    }

    @Override
    public void showInfo() {
        display();
    }

    public void display(){
        System.out.println("ID : "+id+",Name : "+name+",email : "+email+",mobile : "+mobile+",CGPA : "+cgpa+",dept : "+dept+";");
    }

    @Override
    public String toString() {
        return "ID : "+id+",dept : "+dept+";\n";
        }
}

这是代码,什么显示给我,我的结果

@FXML
    private void showStudentButtonOnclick(actionEvent event) {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
             File f = new File("student.bin");
            if(!f.exists())
                outputTextFeild.setText("File Does Not Exists!!");

            else {
                fis = new FileInputStream(f);
                ois = new ObjectInputStream(fis);
                outputTextFeild.setText("");

            try{
//                    Student s;
                    while(true){
                       Student s = (Student) ois.readObject();
                        System.out.println(s.toString());
                        outputTextFeild.appendText(s.toString());
                    }
            } catch (Exception ex){
                    System.out.println(ex);
                }
            }

        } catch (IOException ex) {
//            Logger.getLogger(FXMLMainSceneController.class.getName()).log(Level.SEVERE,null,ex);
            System.out.println(ex);
        }

        finally{
            if(ois != null)
            try{
                 ois.close();
            } catch (IOException ex){
            System.out.println(ex);
        }
        }

    }

但这确实是它返回

ID:0,名称:null,电子邮件:null,移动设备:null,CGPA:2.6,dept:cse;

ID:0,名称:null,电子邮件:null,移动设备:null,CGPA:2.7,dept:bba;

他们无法返回我的超级类数据。 请帮助我。谢谢。

colby63 回答:在具体类中,我的ObjectOutputStream无法正常工作,无法显示正确的结果

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3001298.html

大家都在问