Java ObjectStreamClass getField()方法与示例

ObjectStreamClass类的getField()方法 (ObjectStreamClass Class getField() method)

  • getField() method is available in java.io package.

    getField()方法在java.io包中可用。

  • getField() method is used to return the field of this ObjectStreamClass by the given field name (fi_na).

    getField()方法用于通过给定的字段名称(fi_na)返回此ObjectStreamClass的字段。

  • getField() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    getField()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • getField() method does not throw an exception at the time of returning named field.

    返回命名字段时, getField()方法不会引发异常。

Syntax:

句法:

    public ObjectStreamField getField(String fi_na);

Parameter(s):

参数:

  • String fi_na – represents the name of the returned field.

    字符串fi_na –表示返回字段的名称。

Return value:

返回值:

The return type of the method is ObjectStreamField, it returns the ObjectStreamField object of the named field when exists otherwise it returns null when no named field exists.

该方法的返回类型为ObjectStreamField ,如果存在,它将返回命名字段的ObjectStreamField对象,否则,如果不存在命名字段,则返回null。

Example:

例:

// Java program to demonstrate the example 
// of ObjectStreamField getField(String fi_na)
// method of ObjectStreamClass

import java.io.*;
import java.util.*;

public class GetField {
 public static void main(String[] args) {
  // Instantiates ObjectStreamClass for
  // and Calendar
  ObjectStreamClass o_stm = ObjectStreamClass.lookup(Calendar.class);

  // By using getField() method is to return
  // the field of this class by name
  ObjectStreamField o_sf1 = o_stm.getField("isTimeSet");
  ObjectStreamField o_sf2 = o_stm.getField("fields");

  // Display o_sf1, o_sf2
  System.out.println("o_stm.getField(isTimeSet): " + o_sf1);
  System.out.println("o_stm.getField(fields): " + o_sf2);
 }
}

Output

输出量

o_stm.getField(isTimeSet): Z isTimeSet
o_stm.getField(fields): [I fields


翻译自: https://www.includehelp.com/java/objectstreamclass-getfield-method-with-example.aspx