我有一个我正在研究的项目,而且我需要创建一个抽象类AbstractCustomer
的ArrayList; ArrayList需要由从数据或文本文件读入的AbstractCustomer组成,所以我试图通过数据文件来完成但不能绕过它?
这是我的班级:
import java.io.FileOutputStream;
import java.io.*;
public abstract class AbstractCustomer implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String phoneNumber;
private final int MIN_LENGTH = 10;
public AbstractCustomer() {
}
public AbstractCustomer(String name, String phoneNumber) {
this.name = name;
this.phoneNumber = phoneNumber;
//Phone number is less than 10, but what if it's more than 10?
//Assignment doesn't say, so I will assume it can be more than 10.
//Ya know country codes 1845 and all that
if (phoneNumber.length() < MIN_LENGTH) {
throw new NumberFormatException("Phone number must be at least 10 digits long.");
}
//Parse the long, so this will rule out anything else except digit.
try {
Long.parseLong(phoneNumber);
} catch (NumberFormatException e) {
System.out.println("Phone number can only contain digits, please no dashes ('-') or spaces (' ')");
}
}
abstract double returnDiscountPrice(double itemCost);
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AbstractCustomer other = (AbstractCustomer) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (phoneNumber == null) {
if (other.phoneNumber != null)
return false;
} else if (!phoneNumber.equals(other.phoneNumber))
return false;
return true;
}
@Override
public String toString() {
return "AbstractCustomer[name=" + name + ", phoneNumber=" + phoneNumber + "]";
}
}
这就是我想要做的事情:
public void createDataFile() {
try {
FileOutputStream fout = new FileOutputStream("C:\\Users\\Frank\\Desktop\\Assignment\\abstract.dat");
ObjectOutputStream outputStream = new ObjectOutputStream(fout);
outputStream.writeObject(HOW DO I WRITE THE ABSTRACT CLASS TO A FILE HERE????);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} 任何帮助表示赞赏!
答案 0 :(得分:1)
首先,使用IO编写的是一个对象,而不是一个类。您可以在oracle documentation中阅读:
抽象类无法实例化,但它们可以是子类。
您需要做的是删除抽象标识符(我没有看到任何抽象方法)或继承此抽象类。
只有这样,您才能实例化类的对象(Not)AbstractCustomer并使用IO对它们进行序列化。
您的抽象类是Serializable的唯一原因是扩展It的类也可以序列化。我希望这很清楚。
这是一个序列化的例子:(请注意,我创建了一个继承抽象类的嵌套类。不要这样写。我只是为了演示目的而做的。主方法不适用于那个案例):
// this cannot be serialized because no objects can be instantiated
public abstract class SerializationTest {
// this can be, because it's not abstract
public static class SerializationTestB extends SerializationTest {
public static void main(String[] args) {
// instantiation of an object that you want to write to file
SomeObject obj = new SomeObject(param, param, param);
SomeObject obj2 = new SomeObject(param, param, param);
SomeObject obj3 = new SomeObject(param, param, param);
SomeObject obj4 = new SomeObject(param, param, param);
SomeObject obj5 = new SomeObject(param, param, param);
SomeObject obj6 = new SomeObject(param, param, param);
// an object that represents the path of the file
File f = new File("some/path/here");
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
out.writeObject(obj);
out.writeObject(obj2);
out.writeObject(obj3);
out.writeObject(obj4);
out.writeObject(obj5);
out.writeObject(obj6);
out.flush();
out.close();
}
}
}