📜  Java I/O-ObjectStreamField

📅  最后修改于: 2020-09-27 03:23:28             🧑  作者: Mango

Java ObjectStreamField类

Serializable类中对Serializable字段的描述。 ObjectStreamFields数组用于声明类的Serializable字段。

java.io.ObjectStreamClass.getField(String name)方法通过名称获取此类的字段。

建设者

Constructor Description
ObjectStreamField(String name, Class type) It creates a Serializable field with the specified type.
ObjectStreamField(String name, Class type, boolean unshared) It creates an ObjectStreamField representing a serializable field with the given name and type.

方法

Modifier and Type Method Description
int compareTo(Object obj) It compares this field with another ObjectStreamField.
String getName() It gets the name of this field.
int GetOffset() Offset of field within instance data.
Class getType() It get the type of the field.
char getTypeCode() It returns character encoding of field type.
String getTypeString() It return the JVM type signature.
boolean isPrimitive() It return true if this field has a primitive type.
boolean isUnshared() It returns boolean value indicating whether or not the serializable field represented by this ObjectStreamField instance is unshared.
protected void setOffset(int offset) Offset within instance data.
String toString() It return a string that describes this field.

公共字符getTypeCode()

返回字段类型的字符编码。编码如下:

B byte
C char
D double
F float
I int
J long
L class or interface
S short
Z boolean
[ array

返回值:

可序列化字段的类型代码

例:

import java.io.ObjectStreamClass;
import java.util.Calendar;

public class ObjectStreamClassExample {
 public static void main(String[] args) {
   
      // create a new object stream class for Integers
      ObjectStreamClass osc = ObjectStreamClass.lookup(String.class);

      // get the value field from ObjectStreamClass for integers
      System.out.println("" + osc.getField("value"));

      // create a new object stream class for Calendar
      ObjectStreamClass osc2 = ObjectStreamClass.lookup(Calendar.class);

      // get the Class instance for osc2
      System.out.println("" + osc2.getField("isTimeSet"));

   }
}

输出:

I value
Z isTimeSet