1. 程式人生 > >JAVA中的一些基本知識

JAVA中的一些基本知識

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]