父类可序列化子类可序列化否乎?

[size=medium]关于这个问题,写了个简单的代码测试了下:
可序列化的类Father[/size]
package com.taobao.test;

import java.io.Serializable;

/**
* @author tina.wyn
*
*/
public class Father implements Serializable {

private static final long serialVersionUID = 1L;

private Integer fatherage;

private String str;

public Father() {
fatherage = 50;
str = "I am father";
}

public Integer getNumber() {
return fatherage;
}

public void setNumber(Integer number) {
this.fatherage = number;
}

public String getStr() {
return str;
}

public void setStr(String str) {
this.str = str;
}
}

[size=medium]不可序列化的类Mother[/size]

package com.taobao.test;

public class Mother {

private Integer matherage;

private String str;

public Mother(){
matherage = 30;
str = "I am mother";
}

public Integer getMatherage() {
return matherage;
}

public void setMatherage(Integer matherage) {
this.matherage = matherage;
}

public String getStr() {
return str;
}

public void setStr(String str) {
this.str = str;
}


}

[size=medium]
子类Son继承了父类Father[/size]

package com.taobao.test;

/**
* @author tina.wyn
*
*/
public class Son extends Father {
private static final long serialVersionUID = 1L;


private int age;

private transient String unserialize;

public Son() {
age = 10;
unserialize = "hi";
}


public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}


public String getUnserialize() {
return unserialize;
}


public void setUnserialize(String unserialize) {
this.unserialize = unserialize;
}


}


package com.taobao.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
* @author tina.wyn
*
*/
public class TestSerialize {

public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {

Son s = new Son();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("hello")));
objectOutputStream.writeObject(s);

ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(new File("hello")));
Son sn = (Son) objectInputStream.readObject();

System.out.println(sn.getAge());
System.out.println(sn.getNumber());
System.out.println(sn.getStr());
System.out.println(sn.getUnserialize());
}
}

[size=medium]
运行结果[/size]

10
50
I am father
null

[size=medium]
由此证明父类可序列化子类也可序列化,而sn.getUnserialize()为null是因为子类里将它定义为了[color=red]transient[/color] 类型的,所以子类的属性可否序列化取决于如何定义。
现在我们在子类里加入Mother类的引用[/size]

package com.taobao.test;

/**
* @author tina.wyn
*
*/
public class Son extends Father {
private static final long serialVersionUID = 1L;


private int age;

private transient String unserialize;
Mother mother;

public Son() {
mother = new Mother();
age = 10;
unserialize = "hi";
}


public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}


public String getUnserialize() {
return unserialize;
}


public void setUnserialize(String unserialize) {
this.unserialize = unserialize;
}


public Mother getMother() {
return mother;
}


public void setMother(Mother mother) {
this.mother = mother;
}


}

[size=medium]运行结果:[/size]
  Exception in thread "main" java.io.NotSerializableException: com.taobao.test.Mother
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
at com.taobao.test.TestSerialize.main(TestSerialize.java:21)
[size=medium]
吼吼,出错鸟~Mother类不支持序列化哈~~~[/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值