当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Method类 toGenericString()用法及代码示例


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()方法不包括泛型类型。

参考:




相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Method Class | toGenericString() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。