我有一堂书课:
public abstract class Book
{
private String author, title;
private int pages;
private double price;
Book next;
public Book( String f, String t, int sider, double p )
{
author = a;
title = t;
pages = pa;
price = p;
next = null;
}
以及小说、教科书等子类。我需要一种方法将这些写入带有 DataOutputStream 的文件。我在下面制作了这段代码,但我不确定我是否走在正确的道路上。我将如何在子类中使用它?
public void writeObjectToFile()
{
try
{
FileOutputStream fos = new FileOutputStream("book.something");
DataOutputStream dos = new DataOutputStream(fos);
dos.writeUTF(author);
dos.writeUTF(title);
dos.writeInt(pages);
dos.writeDouble(price);
}catch (IOException e){
e.printStackTrace();
}
}
假设教科书类是这样的:
class Textbook extends Book
{
private String subject;
public Textbook( String a, String t,int pa, double p, String sub )
{
super( a, t, pa, p );
subject = sub;
}
有任何想法吗?我不允许使用ObjectOutputStream
.