方法类 | Java中的 toGenericString() 方法
Method 类的Java.lang.reflect.Method.toGenericString()方法返回一个字符串,该字符串给出 Method 的详细信息,包括方法的类型参数的详细信息。
句法:
public String toGenericString()
返回值:此方法返回一个字符串,该字符串给出了方法的详细信息,包括方法的类型参数的详细信息。
下面的程序说明了 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 Java
- https://docs.oracle.com/javase/8/docs/api/java Java