我的继承代码是否正确?

时间:2014-12-28 16:01:35

标签: java inheritance

所以基本上,我正在做这个学校项目,我有一个名为Person的超类,以及两个扩展类Person的子类(employees和clients)。当我运行eighter子类时,例如当我添加一个员工时,将数据存储在.dat文件中,退出程序并重新运行它,之前存储的所有数据都设置为空字符串。我想这是来自Person类中的无参数构造函数,但这是我学会编写代码的方式。

超级课程:

import java.util.*;
public class Person{
String name, sname, dob, address, tel, mob;

public Person(){
    name = "";  //I tried removing these statements, but then the data returned was null
    sname = "";
    dob = "";
    address = "";
    tel = ""; 
    mob = "";
}

public Person(String name, String sname, String dob, String address, String tel, String mob){
    this.name = name;
    this.sname = sname;
    this.dob = dob;
    this.address = address;
    this.tel = tel;
    this.mob = mob;
} 

子类:

import java.io.*;
import java.util.*;
public class Employee extends Person implements Serializable{
private String empid;

private String valn = "[a-zA-Z ]+";
private String vala = "[a-zA-Z0-9, ]+";
private String valt = "[2][17][0-9]{6}";
private String valm = "[79][9][0-9]{6}";
private String valmm = "[7][7][0-9]{6}";
private String chkeid = "[0-9]{4}[E]";

public Employee(){
    super();
    empid = "";
}

public Employee(String name, String sname,  String dob, String address, String tel,        String mob,String empid){
    super(name, sname, dob, address, tel, mob); 
    this.empid = empid;
}

public void setId(String empid){
    this.empid = empid;
}

public String getId(){
    return empid;
}

public String ToString(){
    return "Emloyee Name: " + name + " " + sname + " " + 
    "\nAddress: " + address +
    "\nDate of Birth: " + dob +
    "\nTelephone Number: " + tel +
    "\nMobile Number:  " + mob +
    "\nEmployee ID:  " + empid;
}//Other methods...

文件保存代码:

File stock = new File("C:/Users/david/Desktop/Stock.dat");
    File employee = new File("C:/Users/david/Desktop/Employee.dat");
    File clients = new File("C:/Users/david/Desktop/Clients.dat");

    try{
        if(stock.exists() == true){
            FileInputStream inFileStream = new FileInputStream (stock); 
            ObjectInputStream inObjectStream = new ObjectInputStream(inFileStream);
            stck = (Vector <Stock>)inObjectStream.readObject(); 
            inObjectStream.close();
        }else{
            FileOutputStream outFile = new FileOutputStream(stock);
            ObjectOutputStream outObject = new ObjectOutputStream(outFile);
            outObject.close();
        }
    }catch (IOException io ){
        System.out.println ("Error Loading Stock Database!");
        sc.next();
    }catch (ClassNotFoundException c){
        System.out.println ("Error Loading Database! Class not found!!");
        sc.next();
    }

    try{
        if(employee.exists() == true){
            FileInputStream inFile = new FileInputStream (employee); 
            ObjectInputStream inObject = new ObjectInputStream(inFile);
            emp = (Vector <Employee>)inObject.readObject(); 
            inObject.close();
        }else{
            FileOutputStream outStr = new FileOutputStream(employee);
            ObjectOutputStream outObj = new ObjectOutputStream(outStr);
            outObj.close();
        }
    }catch (IOException io ){
        System.out.println ("Error Loading Employee Database!");
        sc.next();
    }catch (ClassNotFoundException c){
        System.out.println ("Error Loading Database! Class not found!");
        sc.next();
    }

    try{
        if(clients.exists() == true){
            FileInputStream in = new FileInputStream (clients); 
            ObjectInputStream inObjct = new ObjectInputStream(in);
            clt = (Vector <Clients>)inObjct.readObject(); 
            inObjct.close();
        }else{
            FileOutputStream out = new FileOutputStream(clients);
            ObjectOutputStream outObjct = new ObjectOutputStream(out);
            outObjct.close();
        }
    }catch (IOException io ){
        System.out.println ("Error Loading Clients Database!");
        sc.next();
    }catch (ClassNotFoundException c){
        System.out.println ("Error Loading Database! Class not found!");
        sc.next();
    }


//at the end of the runner class.
try{
        FileOutputStream outFile = new FileOutputStream(stock);
        ObjectOutputStream outObject = new ObjectOutputStream(outFile);
        outObject.writeObject(stck);
        outObject.close();
    }catch(IOException e){
        System.out.println ("Error writing to Database!");
        sc.next();
    }

    try{
        FileOutputStream outFile = new FileOutputStream(employee);
        ObjectOutputStream outObject = new ObjectOutputStream(outFile);
        outObject.writeObject(emp);
        outObject.close();
    }catch(IOException e){
        System.out.println ("Error writing to Database!");
        sc.next();
    }

    try{
        FileOutputStream outF = new FileOutputStream(clients);
        ObjectOutputStream outObjct = new ObjectOutputStream(outF);
        outObjct.writeObject(clt);
        outObjct.close();
    }catch(IOException e){
        System.out.println ("Error writing to Database!");
        sc.next();
    }

3 个答案:

答案 0 :(得分:1)

Employee是可序列化的,但Person不是。这意味着从Person继承的字段没有被序列化(也就是说,它们没有被写入ObjectOutputStream),并且在反序列化期间(即从ObjectInputStream读取)不会被填充。

解决此问题的最简单方法是使Person实现Serializable。请注意,接口是继承的,因此如果您确实进行了更改,则可以安全地从Employee类中删除implements Serializable

如果这不是一个选项,您必须自己在Employee类中完成工作。对于刚接触Java的人来说,这是相当复杂的,所以我宁愿不要把这个答案弄得一塌糊涂,但如果你需要走这条路,我可以详细说明。

答案 1 :(得分:0)

您的代码在继承性方面似乎是正确的,&#34;问题&#34;似乎是你正在使用它的方式。

您如何实现每位员工的安全? 你在使用带参数的构造函数吗?当您重新运行程序时,必须使用该程序,否则您将在描述时实例化空白的empoloyees。

答案 2 :(得分:0)

为了序列化超类的成员,这个Person类也应该实现Serializable。

 public class Person implements Serializable {
 ...

如上所述here,那么Employee类将自动实现Seralizable。