我刚从这个话题开始。我的理解是,如果你想保存一个类的实例变量,以便以后可以使用它......你可以使用没有定义方法的“可序列化”接口......但我怀疑,发生了什么继承...假设我的超类未序列化而子类已序列化...我可以保存我的超类实例变量吗?虽然,我知道这一点......超类的构造函数被调用并且它将运行......但是这是什么意思?这是否意味着,我无法保存超类的实例变量......
在下面的程序中......创建的文件将存储数值(43,23)或只是变量“高度”和“重量”???
import java.io.*;
class A {
String sex;
public void gone(String sex){
this.sex = sex;
System.out.println("i m " + sex);
}
}
public class serialio1 extends A implements Serializable {
int height;
int weight;
public static void main(String[] args){
serialio1 myBox = new serialio1();
myBox.go(43,23);
try{
FileOutputStream fs = new FileOutputStream("foo.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(myBox);
os.close();
FileInputStream fileStream = new FileInputStream("foo.ser");
ObjectInputStream ms = new ObjectInputStream(fileStream);
Object one = ms.readObject();
Object two = ms.readObject();
int h = (int) one;
int w = (int) two;
ms.close();
System.out.println("saved values" + h + w);
} catch(Exception ex){
ex.printStackTrace();
}
}
public void go(int height , int weight) {
this.height = height;
this.weight = weight;
System.out.println(" height is " + height);
System.out.println(" weight is " + weight);
}
}