下面代码实现了Vehicle类对象的序列化,要求文件存放在在D盘根目录下,文件名为a.txt,请完成以下空白代码。注意代码大小写,不要写多余的代码。
import java.io.*;
public class TestMain
{
public static void main(String[] args)
{
Vehicle veh = new Vehicle(7, 80, 11);
try ( ObjectOutputStream oos = (1) )
{
(2) ;
}
catch (IOException ex)
{ ex.printStackTrace(); }
try (ObjectInputStream ois = (3) )
{
Vehicle ve = (4) ;
System.out.println(ve);
}
catch (IOException | ClassNotFoundException ex) {
ex.printStackTrace();
}
}
}
class Vehicle (5)
{
int passengers; // 乘员数量
int fuelcap; // 油箱容量
int mpl; // 平均油耗
Vehicle(int p, int f, int m)
{
passengers = p;
fuelcap = f;
mpl = m;
}
int range() {return fuelcap * mpl; }
double fuelneeded(int km) {return (double) km / mpl; }
public String toString()
{
return ("Vehicle 对象,成员:" + passengers + ",油箱容量:" + fuelcap + ",平均油耗:" + mpl);
}
}
对象流的应用,输入输出对象流创建,对象继承Seriallizable接口