我有一个 Book 类和一个扩展 Book 的 Library Book 类。我将信息存储在随机访问文件中。我有一个 writeToFile 方法,它将 Book 对象写入随机访问文件。我的 LibraryBook 类的 writeToFile 方法调用 super.writeToFile,然后我希望它将特定于 LibraryBook 的字段写入文件。这样做的正确方法是什么?见代码:
book 类的方法:
public void writeToFile(String fileName, long location) throws Exception {
try {
RandomAccessFile invFile = new RandomAccessFile(fileName, "rw");
// seek to correct record in file
invFile.seek(location);
// now write out the record
// write out the data in fixed length fields
// String fields must be truncated if too large
// or padded with blanks if too small
//write out all Book variable to file
invFile.writeLong(ISBN);
//etc.....
} catch (FileNotFoundException notFound) {
throw new FileNotFoundException();
} catch (IOException io) {
throw io;
}
}
扩展 Book 的 LibraryBook 类中的方法:
public void writeToFile(String fileName, long location) throws Exception {
try {
super.writeToFile(fileName, location);
RandomAccessFile invFile = new RandomAccessFile(fileName, "rw");
// seek to correct record in file
invFile.seek(location);
// now write out the record
// write out the data in fixed length fields
// String fields must be truncated if too large
// or padded with blanks if too small
//write library book variables to file
invFile.writeLong(branchID);
//etc....
invFile.close();
} catch (FileNotFoundException notFound) {
throw new FileNotFoundException();
} catch (IOException io) {
throw io;
}
}
如何对其进行编码,以便 LibraryBook writeToFile 方法可以调用超类方法并将 LibraryBook 保存到文件中?