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


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


java.lang.reflect.Method类的getName()方法有助于以String的形式获取方法的名称。要获取类的所有方法的名称,请获取该类对象的所有方法。然后在这些方法对象上调用getName()。

用法:

public String getName()

返回值:它以String形式返回方法的名称。



例:

Method:public void getValue(){}
getName() returns: getValue
Explanation:The getName() function on object of above method returns name of method
which is getValue.

Method:public void paint(){}
getName() returns: paint

以下示例程序旨在说明Method类的getName()方法:

范例1:打印方法对象的所有方法的名称。

/* 
* Program Demonstrate how to apply getName() method 
* of Method Class. 
*/
import java.lang.reflect.Method; 
  
public class GFG { 
  
    // Main method 
    public static void main(String[] args) 
    { 
  
        try { 
            // create class object 
            Class classobj = GFG.class; 
  
            // get list of methods 
            Method[] methods = classobj.getMethods(); 
  
            // get the name of every method present in the list 
            for (Method method:methods) { 
  
                String MethodName = method.getName(); 
                System.out.println("Name of the method:"
                                   + MethodName); 
            } 
        } 
        catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
  
    // method name setValue 
    public static int setValue() 
    { 
        System.out.println("setValue"); 
        return 24; 
    } 
  
    // method name getValue 
    public String getValue() 
    { 
        System.out.println("getValue"); 
        return "getValue"; 
    } 
  
    // method name setManyValues 
    public void setManyValues() 
    { 
        System.out.println("setManyValues"); 
    } 
}
输出:
Name of the method:main
Name of the method:getValue
Name of the method:setValue
Name of the method:setManyValues
Name of the method:wait
Name of the method:wait
Name of the method:wait
Name of the method:equals
Name of the method:toString
Name of the method:hashCode
Name of the method:getClass
Name of the method:notify
Name of the method:notifyAll

范例2:用于检查类是否包含特定方法的程序。

/* 
* Program Demonstrate how to apply getName() method 
* of Method Class within a class 
*/
import java.lang.reflect.Method; 
  
public class GFG { 
  
    // Main method 
    public static void main(String[] args) 
    { 
  
        String checkMethod = "method1"; 
  
        try { 
            // create class object 
            Class classobj = democlass.class; 
  
            // get list of methods 
            Method[] methods = classobj.getMethods(); 
  
            // get the name of every method present in the list 
            for (Method method:methods) { 
  
                String MethodName = method.getName(); 
                if (MethodName.equals(checkMethod)) { 
                    System.out.println("Class Object Contains"
                                       + " Method whose name is "
                                       + MethodName); 
                } 
            } 
        } 
        catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
} 
// a simple class 
class democlass { 
  
    public int method1() 
    { 
        return 24; 
    } 
  
    public String method2() 
    { 
        return "Happy hours"; 
    } 
  
    public void method3() 
    { 
        System.out.println("Happy hours"); 
    } 
}
输出:
Class Object Contains Method whose name is method1

参考:
https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getName-




相关用法


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