Method類的java.lang.reflect.Method.toGenericString()方法返回一個字符串,該字符串提供Method的詳細信息,包括該方法的類型參數的詳細信息。
用法:
public String toGenericString()
返回值:此方法返回一個字符串,其中提供了Method的詳細信息,包括該方法的類型參數的詳細信息。
以下示例程序旨在說明Method類的toGenericString()方法:
範例1:
// Program Demonstrate toGenericString() method
// of Method Class.
import java.lang.reflect.Method;
public class GFG {
// create another method
public final void paint(Object... values)
{
String message = "A Computer Science portal for geeks";
}
// create main method
public static void main(String args[])
{
try {
// get list of declared method objects of class GFG
Method[] methods = GFG.class.getMethods();
// loop through method list
for (Method method:methods) {
// print only for main and paint methods
if (method.getName().equals("main")
|| method.getName().equals("paint")) {
// print method details using toGenericString()
System.out.println(method.toGenericString());
}
}
}
catch (Exception e) {
// Print Exception if any Exception occurs
e.printStackTrace();
}
}
}
輸出:
public static void GFG.main(java.lang.String[]) public final void GFG.paint(java.lang.Object...)
範例2:
說明:在此方法中,首先創建java.util.concurrent.CountDownLatch類對象。創建java.util.concurrent.CountDownLatch類的類對象後,通過調用類Object的getMethods()創建方法對象的列表。使用toGenericString()遍曆“方法”列表並打印方法詳細信息。
// Program Demonstrate toGenericString() method
// of Method Class.
import java.lang.reflect.Method;
import java.util.concurrent.CountDownLatch;
public class GFG {
// create main method
public static void main(String args[])
{
try {
// create class object for class CountDownLatch
Class c = CountDownLatch.class;
// get list of Method object
Method[] methods = c.getMethods();
System.out.println("Methods of CountDownLatch:");
// Loop through Methods list
for (Method m:methods) {
// Print Method details
System.out.println("Method:"
+ m.toGenericString());
}
}
catch (Exception e) {
// print Exception is any Exception occurs
e.printStackTrace();
}
}
}
輸出:
Methods of CountDownLatch: Method:public boolean java.util.concurrent.CountDownLatch.await(long,java.util.concurrent.TimeUnit) throws java.lang.InterruptedException Method:public void java.util.concurrent.CountDownLatch.await() throws java.lang.InterruptedException Method:public void java.util.concurrent.CountDownLatch.countDown() Method:public long java.util.concurrent.CountDownLatch.getCount() Method:public java.lang.String java.util.concurrent.CountDownLatch.toString() Method:public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException Method:public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException Method:public final void java.lang.Object.wait() throws java.lang.InterruptedException Method:public boolean java.lang.Object.equals(java.lang.Object) Method:public native int java.lang.Object.hashCode() Method:public final native java.lang.Class> java.lang.Object.getClass() Method:public final native void java.lang.Object.notify() Method:public final native void java.lang.Object.notifyAll()
說明:該程序的輸出還顯示除CountDownLatch類中定義的方法以外的方法對象的結果,例如wait,equals,toString,hashCode,getClass,notify,notifyAll。這些方法均繼承自java.lang包的超類名稱Object。
toGenericString()和toString()之間的區別
- toGenericString()返回描述此方法的字符串,包括其通用類型參數。
- toString()返回描述此方法的字符串。
- toString()方法不包括泛型類型。
參考:
- https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#toString-
- https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#toGenericString-
相關用法
- Java Class toGenericString()用法及代碼示例
- Java Field toGenericString()用法及代碼示例
- Java Constructor toGenericString()用法及代碼示例
- Java Method類 getGenericReturnType()用法及代碼示例
- Java Method類 getDeclaringClass()用法及代碼示例
- Java Method類 getGenericParameterTypes()用法及代碼示例
- Java Method類 getGenericExceptionTypes()用法及代碼示例
- Java Method類 getName()用法及代碼示例
- Java Method類 getReturnType()用法及代碼示例
- Java Method類 getParameterTypes()用法及代碼示例
- Java Method類 getAnnotatedReturnType()用法及代碼示例
- Java Method類 getParameterAnnotations()用法及代碼示例
- Java Method類 equals()用法及代碼示例
- Java Method類 getParameterCount()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Method Class | toGenericString() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。