📌  相关文章
📜  Java的.lang.reflect.Method类在Java中

📅  最后修改于: 2022-05-13 01:54:32.511000             🧑  作者: Mango

Java的.lang.reflect.Method类在Java中

Java.lang.reflect.Method类提供有关特定类别或接口上的一种方法的必要详细信息,并提供对其的访问。反射方法也可以是类别方法或实例方法(包括抽象方法)。

此类允许在匹配实际参数以调用基础方法的形式参数时发生扩大转换,但如果发生缩小转换,则会引发 IllegalArgumentException。

方法:

MethodDescription
equals(Object obj)This method compares this Method against the specified object.
getAnnotatedReturnType()This method returns an Annotated Type object that represents the use of a type to specify the return type of the method/constructor represented by this Executable.
getAnnotation(Class annotationClass)This method returns this element’s annotation for the specified type if such an annotation is present, else null.
getDeclaredAnnotations()This method returns annotations that are directly present on this element.
getDeclaringClass()This method returns the Class object representing the class or interface that declares the executable represented by this object.
getDefaultValue()This method returns the default value for the annotation member represented by this Method instance.
getExceptionTypes()This method returns an array of Class objects that represent the types of exceptions declared to be thrown by the underlying executable represented by this object.
getGenericExceptionTypes()This method returns an array of Type objects that represent the exceptions declared to be thrown by this executable object.
getGenericParameterTypes()This method returns an array of Type objects that represent the formal parameter types, in declaration order, of the executable represented by this object.
getGenericReturnType()This method returns a Type object that represents the formal return type of the method represented by this Method object.
getModifiers()This method returns the Java language modifiers for the executable represented by this object.
getName()This method returns the name of the method represented by this Method object, as a String.
getParameterAnnotations()This method returns an array of arrays of Annotations that represent the annotations on the formal parameters, in declaration order, of the Executable represented by this object.
getParameterCount()This method returns the number of formal parameters (whether explicitly declared or implicitly declared or neither) for the executable represented by this object.
getParameterTypes()This method returns an array of Class objects that represent the formal parameter types, in declaration order, of the executable represented by this object.

getDefaultValue() 方法:

Java
// Java program to show the
// Implementation of getDefaultvalue()
  
import java.lang.reflect.Method;
  
public class getDefaultValueExample {
  
    public static void main(String[] args)
    {
  
        Method[] methods = Demo.class.getMethods();
  
        // calling method getDefaultValue()
        System.out.println(methods[0].getDefaultValue());
    }
}
  
class Demo {
    private String str;
  
    // member function returning string str
    public String getSampleField() { return str; }
  
    public void setSampleField(String str)
    {
        this.str = str;
    }
}


Java
// Java program to show the
// Implementation of toString()
  
import java.lang.reflect.Method;
  
public class MethodDemo {
    public static void main(String[] args)
    {
  
        Method[] methods = SampleClass.class.getMethods();
  
        // calling method toString()
        System.out.println(methods[1].toString());
    }
}
  
class SampleClass {
    private String str;
  
    public String getSampleField() { return str; }
  
    public void setSampleField(String str)
    {
        this.str = str;
    }
}


输出
null

toString() 方法:

Java

// Java program to show the
// Implementation of toString()
  
import java.lang.reflect.Method;
  
public class MethodDemo {
    public static void main(String[] args)
    {
  
        Method[] methods = SampleClass.class.getMethods();
  
        // calling method toString()
        System.out.println(methods[1].toString());
    }
}
  
class SampleClass {
    private String str;
  
    public String getSampleField() { return str; }
  
    public void setSampleField(String str)
    {
        this.str = str;
    }
}
输出
public void SampleClass.setSampleField(java.lang.String)