1.进行学生的帐号注册和登录时会出现标题的错误,可是我强制转换时,并不是从Administrator转换为Student类,而是从Object类强制转换为Student类
2.这是Student类的代码
package elective.system;
import java.io.Serializable;
/*
学生类
*/
public class Student implements Serializable {
private static final long serialVersionUID = 5816167009388425995L;
private String number; //账号
private String passWord; //密码
public Student(String number, String passWord) {
this.number = number;
this.passWord = passWord;
}
public Student() {
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
//选择选修课程
public boolean select_class(){
return true;
}
//查看自己所选课程
public void look_class(){
}
//查找课程
public void seek_class(){
}
}
这是文件读取和保存的类
package elective.system;
import java.io.*;
import java.util.ArrayList;
/*
文件操作类
*/
public class OperateFile implements Serializable{
public boolean save(Object e, File file, Boolean flag2) throws Exception{ // 保存对象
boolean flag = false ; // 定义操作标志位
FileOutputStream fos = new FileOutputStream(file,flag2); //采用追加方式写入数据
if(file.length()<1){
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(e);
flag = true;
oos.close();
}else{
MyObjectOutputStream mos = new MyObjectOutputStream(fos);
mos.writeObject(e);
flag = true;
mos.close();
}
return flag ;
}
public ArrayList load(File file) throws Exception{ // 读取对象
ArrayList<Object> list = new ArrayList();
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
while(fis.available()>0){
Object p = ois.readObject();
list.add(p);
}
ois.close();
return list;
}
}
class MyObjectOutputStream extends ObjectOutputStream{
public MyObjectOutputStream(OutputStream out) throws IOException {
super(out);
}
public void writeStreamHeader() throws IOException{
return;
}
}
这是注册和登录的方法
public boolean enter2(String number, String passWord,String pathName) throws Exception {
File file = new File(pathName);
OperateFile operateFile = new OperateFile();
ArrayList<Object> list = operateFile.load(file);
for (Object ot:list) {
Student ar = (Student) ot;
if(ar.getNumber().equals(number)&&ar.getPassWord().equals(passWord)){
return true;
}
}
return false;
}
public boolean sign2(String number,String passWord,String pathName) throws Exception {
File file = new File(pathName);
OperateFile operateFile = new OperateFile();
ArrayList<Object> list = operateFile.load(file);
for(Object ot : list){
Student ar = (Student) ot;
if(ar.getNumber().equals(number)){
return false;
}
}
Object ar = new Student(number, passWord);
operateFile.save(ar,file,true);
return true;
}
请问大佬们这到底是为什么啊