tInt()和 setDouble()分别给这两个变量赋值和用toString()方法显示对象的私有成员变量的值,要求将这个类定义为Serializable,再创建这个类的持久对象,将对象写入名为ex3_2.data的文件中并显示对象的2个成员变量,最后从这个文件中读取这个对象并显示对象的2个成员变量。
注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。程序运行结果如下:
ex3_2: i=123 d=123.456
ex3_2: i=123 d=123.456
import java.io.*;
public class ex3_2 implements Serializable {
private int n3_2;
private double d3_2;
ex3_2() {
n3_2 = 25;
d3_2 = 12.23;
}
public void setDouble(double x) {
d3_2 = x;
}
public void setInt(int x) {
n3_2 = x;
}
public String toString() {
return("ex3_2: i=" + n3_2 + "d=" + d3_2);
}
public static void main(String[] arg) {
ex3_2 e1 = new ex3_2 );
ex3_2 e2;
e1.setInt(123);
e1.setDouble(123.456);
System.out.println(e1);
try {
FileOutputStream fos3_2 = new FileOutputStream("ex3_2.data");
ObjectOutputStream oos3_2 = new ObjectOutputStream(fos3_2);
________________;
} catch(IOException ioe) {
System.out.println(ioe);
}
try {
FileInputStream fis3_2 = new FileInputStream("ex3_2.data");
ObjectInputStream ois3_2 = new ObjectInputStream(fis3_2);
________________;
System.out.println(e2);
}catch(10Exception ioe) {
System.out.println(ioe);
}catch(ClassNotFoundException cnfe) {
System.out.println(cnfe);
}
}
}