當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Field toString()用法及代碼示例


java.lang.reflect.Field的toString()方法用於獲取描述此Field的字符串。返回字符串的格式是該字段的訪問修飾符(如果有),然後是字段類型,然後是空格,然後是聲明該字段的類的完全限定名稱,然後是句點,然後是字段名稱。

例如:

public static final Month java.time.Month.APRIL

修飾符按照“ Java語言規範”中指定的規範順序放置。首先是公共的,受保護的或私有的,然後是其他修飾符,其順序如下:靜態,最終,瞬態,易失。


用法:

public String toString()

參數:此方法不接受任何內容。

返回值:此方法返回描述此字段的字符串。

以下示例程序旨在說明toString()方法:
程序1:

// Java program to illustrate toString() method 
  
import java.lang.reflect.Field; 
  
public class GFG { 
  
    public static void main(String[] args) 
        throws Exception 
    { 
  
        // Get the array of field object 
        Field[] fields = User.class.getFields(); 
  
        // loop 
        for (int i = 0; i < fields.length; i++) { 
  
            // get the string representation of each field 
            String field = fields[i].toString(); 
  
            System.out.println("Field: " + field); 
        } 
    } 
} 
  
// sample User class 
class User { 
  
    // static double values 
    public static double Marks = 34.13; 
    public static float Fees = 3413.99f; 
    public static String name = "Aman"; 
  
    public static double getMarks() 
    { 
        return Marks; 
    } 
  
    public static void setMarks(double marks) 
    { 
        Marks = marks; 
    } 
  
    public static float getFees() 
    { 
        return Fees; 
    } 
  
    public static void setFees(float fees) 
    { 
        Fees = fees; 
    } 
  
    public static String getName() 
    { 
        return name; 
    } 
  
    public static void setName(String name) 
    { 
        User.name = name; 
    } 
}
輸出:
Field: public static double User.Marks
Field: public static float User.Fees
Field: public static java.lang.String User.name

程序2:

// Java program to illustrate toString() 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 Month class 
        Field[] fields = Month.class.getFields(); 
  
        for (int i = 0; i < fields.length; i++) { 
  
            // print name of Fields 
            System.out.println("toString of Field:\n"
                               + fields[i].toString()); 
        } 
    } 
}
輸出:

toString of Field:
public static final java.time.Month java.time.Month.JANUARY
toString of Field:
public static final java.time.Month java.time.Month.FEBRUARY
toString of Field:
public static final java.time.Month java.time.Month.MARCH
toString of Field:
public static final java.time.Month java.time.Month.APRIL
toString of Field:
public static final java.time.Month java.time.Month.MAY
toString of Field:
public static final java.time.Month java.time.Month.JUNE
toString of Field:
public static final java.time.Month java.time.Month.JULY
toString of Field:
public static final java.time.Month java.time.Month.AUGUST
toString of Field:
public static final java.time.Month java.time.Month.SEPTEMBER
toString of Field:
public static final java.time.Month java.time.Month.OCTOBER
toString of Field:
public static final java.time.Month java.time.Month.NOVEMBER
toString of Field:
public static final java.time.Month java.time.Month.DECEMBER

參考文獻: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#toString-



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Field toString() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。