我刚开始讨论这个话题。我的理解是,如果你想保存一个类的实例变量,以便稍后可以使用...你可以使用“可序列化”接口,但没有定义方法......但我怀疑,会发生什么在继承......假设我的超类没有序列化wheras子类被序列化......我可以保存我的超类实例变量吗? 虽然,我知道这个......超类的构造函数正在被调用,它会运行......但是dat的意思是什么?是否意味着,我无法保存超类的实例变量....子类是可序列化的超类不可序列化
在下面的程序中...创建的文件将存储数值(433)或只是变量“高度”和“重量“???
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);
}
}
[tag:javascript]标记已被删除。我没有看到你的问题与JavaScript有什么关系。你可能会用Java来混淆它吗?另外,请考虑格式化您的发布代码。所有左对齐的代码都很难阅读和理解。 –
可能重复的[java对象序列化和继承](http://stackoverflow.com/questions/8632148/java-object-serialization-and-inheritance) –
而且我得到异常....我不知道ÿ? – user2985842