Java Field 到通用字符串()的方法及实例

Java Field 到通用字符串()的方法及实例

java.lang.reflect.FieldtoGenericString() 方法用于返回一个表示该Field的字符串,包括其通用类型。字符串的格式是该字段的访问修饰符(如果有的话),后面是通用字段类型,后面是空格,后面是声明该字段的类的全称,后面是句号,后面是字段名。修饰符按照 “Java语言规范 “规定的标准顺序放置。这就是首先是public、protected或private,然后是其他修饰符,顺序如下:static、final、transient、volatile。

语法

public String toGenericString()

参数: 该方法不接受任何东西。
返回 :该方法返回一个描述该字段的 字符串 ,包括其通用类型。下面的程序说明了toGenericString()方法。

程序1 :

// Java program to illustrate
// toGenericString() method
 
import java.lang.reflect.Field;
import java.time.Month;
 
public class GFG {
 
    public static void main(String[] args)
        throws Exception
    {
 
        // Get all field objects of the Month class
        Field[] fields
            = Month.class.getFields();
 
        for (int i = 0; i < fields.length; i++) {
 
            // print name of Fields
            System.out.println(
                "toGenericString of Field:\n"
                + fields[i].toGenericString());
        }
    }
}

输出:

toGenericString of Field: public static final java.time.Month java.time.Month.January toGenericString of Field: public static final java.time.Month java.time.Month.FebRUARY toGenericString of Field: public static final java.time.Month java.time.Month.March toGenericString of Field: public static final java.time.Month java.time.March.toGenericString of Field: public static final java.time.Month java.time.Month.APRIL toGenericString of Field: public static final java.time.Month java.time.Month.May toGenericString of Field: public static final java.time.Month java.time.Month.toGenericString of Field: public static final java.time.Month java.time.Month.July toGenericString of Field: public static final java.time.Month java.time.Month.AUGUST toGenericString of Field: public static final java.time.Month java.time.Month.toGenericString of Field: public static final java.time.Month java.time.Month.OctOBER toGenericString of Field: public static final java.time.Month java.time.Month.NOVEMBER toGenericString of Field: public static final java.time.Month java.time.Month.DecEMBER

程序2 :

// Java program to illustrate
// toGenericString() method
 
import java.lang.reflect.Field;
 
public class GFG {
 
    public static void main(String[] args)
        throws Exception
    {
 
        // create Numbers object
        Numbers no = new Numbers();
 
        // Get the value field object
        Field field
            = Numbers.class.getField("value");
 
        // print value of isActive
        System.out.println(
            "toGenericString is\n"
            + field.toGenericString());
    }
}
 
// sample Numbers class
class Numbers {
 
    // static short value
    public static short value = 13685;
}

输出

toGenericString is 
public static short Numbers.value

参考文献 : https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#toGenericString-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程