1. Enum和String的相互转化
见例子:
enum PathExceptionType {
EmpytPath,
EmptyMethodPathChain,
EmptyUnitChain,
ReviseError,
NullUnitNode,
NullUnitNodeToString,
DeadLoop,
Unkown
}
//Enum转String
PathExceptionType type=PathExceptionType.DeadLoop;
System.out.println(type.name());
//String转enum
PathExceptionType type2=Enum.valueOf(PathExceptionType.class, "NullUnitNodeToString");
System.out.println(type2.name());
2.对象序列化接口Serializable 遇到的问题
(1)如果A类是可序列化的,实现了Serializable,但是包涵不可序列化的成员,则会导致序列化的失败。
public class Base implements Serializable{
String name;
protected Base(String s)
{
name=s;
}
}
public class NotSerializable{
String name;
int age;
public NotSerializable(String n,int a)
{
name=n;
age=a;
}
}
public class Test extends Base{
String type;
NotSerializable obj;
protected Test(String s,NotSerializable obj)
{
super(s);
this.obj=obj;
type=s;
}
public static void main(String args[]) throws FileNotFoundException, IOException, ClassNotFoundException
{
Test test=new Test("test",new NotSerializable("notseria",1));
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("file.txt"));
out.writeObject(test);
ObjectInputStream in=new ObjectInputStream(new FileInputStream("file.txt"));
Test test2=(Test) in.readObject();
System.out.println(test2.type);
System.out.println(test2.name);
System.out.println(test2.obj.age);
System.out.println(test2.obj.name);
}
}
输出:
Exception in thread “main” java.io.NotSerializableException: NotSerializable
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at Test.main(Test.java:31)
当NotSerializable 变成可序列化时,输出正常,即:
public class NotSerializable implements Serializable
。。。。
输出:
test
test
1
notseria
(2)序列化时的另一种错误
父类不可序列化,子类可序列化。当序列化子类时,需要保证父类有一个可访问的无参构造函数
public class Base {
String name;
/*Base(){}*/
protected Base(String s)
{
name=s;
}
}
public class Test extends Base implements Serializable{
String type;
NotSerializable obj;
protected Test(String s,NotSerializable obj)
{
super(s);
this.obj=obj;
type=s;
}
public static void main(String args[]) throws FileNotFoundException, IOException, ClassNotFoundException
{
Test test=new Test("test",new NotSerializable("notseria",1));
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("file.txt"));
out.writeObject(test);
ObjectInputStream in=new ObjectInputStream(new FileInputStream("file.txt"));
Test test2=(Test) in.readObject();
System.out.println(test2.type);
System.out.println(test2.obj.age);
System.out.println(test2.obj.name);
}
}
运行结果:
Exception in thread “main” java.io.InvalidClassException: Test; no valid constructor
at java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(Unknown Source)
at java.io.ObjectStreamClass.checkDeserialize(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at Test.main(Test.java:34)
当Base加上无参构造函数后,结果如下:
test
1
notseria
关于这点:jdk api文档说明如下:
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.
(3)序列化协议
几种序列化协议
1. protobuf
2. xstream
3. jackjson
4. jdk
5. hessian
关于这些协议的具体信息见:[http://www.tuicool.com/articles/26naqer]