📜  Java IO 对象流字段

📅  最后修改于: 2022-05-13 01:55:38.665000             🧑  作者: Mango

Java IO 对象流字段

Java。伊奥。 ObjectStreamField 类是来自 Serializable 接口的 Serializable 字段的描述。这个类是类的序列化描述符。 ObjectStreamFields 数组用于维护类的可序列化字段。它包括类的名称和 serialVersionUID。

Java IO ObjectStreamField 的类声明如下:

1. public class ObjectStreamField extends Object       // extends Keyword is used  

2. public class ObjectStreamField implements Comparable   // implements Keyword is used

Java IO ObjectStreamField 的构造函数

以下是Java IO ObjectStreamField 类的两个构造函数——

Sr.

No.

ConstructorDescription
1ObjectStreamField (String name, Class type)This Constructor Creates a Serializable field with the specified type to it                                                                
2ObjectStreamField (String name, Class type, boolean unshared)                                                                                                      This Constructor Creates an ObjectStreamField representing a serializable field with the given name and type of it

Java IO ObjectStreamField 的方法

S.No.     Method Method  Type                                                    Description      
1compareTo(Object obj)                                     int This Method is Comparing this field with another ObjectStreamField
2getName() String This method gives the name of the field
3GetOffset() int This method returns the offset of the field within instance data
4getType() ClassIt gives the type of the field for the get type method
5getTypeCode() char This method returns the character encoding of the field type
6getTypeString() String This method returns the JVM type Signature
7isPrimitive() boolean It returns true if the field has a primitive type
8isUnshared() boolean It returns a boolean value indicating whether or not the serializable field represented by this ObjectStreamField instance is unshared.
9setOffset( int offset)                           protected void This method returns offset within the instance of the data
10toString()  String It returns a string that describes all this field

例子:

Java
// Java program to illustrate the working
// of the Java IO ObjectStreamField class
import java.io.ObjectStreamClass;  
import java.util.Calendar;  
    
public class ObjectStreamClassExample
{  
     public static void main(String[] args)
     {  
               
          // creating a new object for the stream class for the Integers  
          ObjectStreamClass abc = ObjectStreamClass.lookup(String.class);  
    
          // getting the value field from ObjectStreamClass for the integers  
          System.out.println("" + abc.getField("value"));  
    
          // creating new object stream class for the  Calendar  
          ObjectStreamClass abc2 = ObjectStreamClass.lookup(Calendar.class);  
    
          // get the Class instance for abc2  
          System.out.println("" + abc2.getField("isTimeSet"));  
    
       }  
}


Java
// Java program to illustrate the working of the
// Java IO ObjectStreamField(compareTo(Object obj)) class
import java.io.*;
  
public class ObjectStreamField_ShowDemo {
     
   public static void main(String[] args) {
     
      // creating a new object stream class for The Integers
      ObjectStreamClass xyz = ObjectStreamClass.lookupAny(Integer.class);
  
      // getting the field value from Integer class
      ObjectStreamField field = xyz.getField("value");
  
      // creating a new object stream class for floats
      ObjectStreamClass xyz2 = ObjectStreamClass.lookupAny(Float.class);
  
      // getting the field value from Integer class
      ObjectStreamField field2 = xyz.getField("value");
  
      // comparing with another field
      System.out.println("" + field.compareTo(field2));
        
   }
}


输出 -

I value
Z isTimeSet

Java IO ObjectStreamField ( compareTo(Object obj) ) 的方法示例 –

下图解释了Java.io.ObjectStreamField.compareTo()方法的用处——

Java

// Java program to illustrate the working of the
// Java IO ObjectStreamField(compareTo(Object obj)) class
import java.io.*;
  
public class ObjectStreamField_ShowDemo {
     
   public static void main(String[] args) {
     
      // creating a new object stream class for The Integers
      ObjectStreamClass xyz = ObjectStreamClass.lookupAny(Integer.class);
  
      // getting the field value from Integer class
      ObjectStreamField field = xyz.getField("value");
  
      // creating a new object stream class for floats
      ObjectStreamClass xyz2 = ObjectStreamClass.lookupAny(Float.class);
  
      // getting the field value from Integer class
      ObjectStreamField field2 = xyz.getField("value");
  
      // comparing with another field
      System.out.println("" + field.compareTo(field2));
        
   }
}

输出 :

0