Serialization中的readObjectNoData

本文介绍了JDK1.4中的readObjectNoData方法,并通过一个具体的示例讲解了该方法如何在类结构发生变化后仍能正确反序列化原有数据。示例中,通过在新的类结构中实现readObjectNoData方法,可以为缺失的数据字段设置默认值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

readObjectNoData方法是JDK1.4中增加的. 但一直以来, 国内外的书上都只是提到而已, 没有真正涉及到例子.

今天有学生问到其用法, 说明一下.

readObjectNoData 
  原始情况
    pojo
		public class Person implements Serializable {               
			private int age;                
			public Person() {  }              
			//setter getter...      
		}
    序列化
         Person p = new Person();             
		 p.setAge(10);             
		 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("c:/person.ser"));             
		 oos.writeObject(p);            
		 oos.flush();             
		 oos.close();
  类结构变化后, 序列化数据不变     pojo
      Animal
        implements Serializable
        显式编写readObjectNoData
        public class Animal implements Serializable {                  
				private String name;                 
				public Animal() {  }                 
				//setter getter...                 
				private void readObjectNoData() {                       
					this.name = "zhangsan";                 
				}          
		}
      Person 
        extends Animal
        public class Person extends Animal implements Serializable {                  
				private int age;                  
				public Person() {  }                 
				// setter getter...         
		}
    反序列化
      ObjectInputStream ois = new ObjectInputStream(new FileInputStream("c:/person.ser"));       
	  Person sp = (Person) ois.readObject();       
	  System.out.println(sp.getName());
      readObject时, 会调用readObjectNoData


 

 

 

 

 

 


 

Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable. To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class's state. It is an error to declare a class Serializable if this is not the case. The error will be detected at runtime. During deserialization, the fields of non-serializable classes will be initialized using the public or protected no-arg constructor of the class. A no-arg constructor must be accessible to the subclass that is serializable. The fields of serializable subclasses will be restored from the stream. When traversing a graph, an object may be encountered that does not support the Serializable interface. In this case the NotSerializableException will be thrown and will identify the class of the non-serializable object. Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures: private void writeObject(java.io.ObjectOutputStream out) throws IOException private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException; private void readObjectNoData() throws ObjectStreamException;
07-23
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值