📜  方法类 | Java中的 getDeclaringClass() 方法

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

方法类 | Java中的 getDeclaringClass() 方法

Method 类的Java.lang.reflect.Method.getDeclaringClass()方法返回类或接口的 Class 对象,该对象定义了我们应用此 getDeclaringClass() 方法的方法。

句法:

public Class getDeclaringClass()

返回值:此方法返回声明方法的Class 对象

下面的程序说明了 Method 类的 getDeclaringClass() 方法:

程序1:通过getDeclaringClass()方法来查找定义方法的类Object的详细信息。

在下面的程序中,创建了一个带有一些方法的 Demo 类。创建类对象后,调用类对象的getMethods()函数创建方法对象列表。然后它们在列表中循环,并打印每个方法的声明类的详细信息。
该程序的输出还显示方法对象的结果,而不是类对象中定义的方法,例如 wait、equals、toString、hashCode、getClass、notify 和 notifyAll。这些方法由类对象继承自Java.lang 包的超类 Object。

// Program Demonstrate getDeclaringClass() method
// of Method Class.
  
import java.lang.reflect.Method;
  
// sample class containing some method
class GFGDemoClass {
  
    // create attributes
    private String field1;
    private String field2;
  
    // create methods
    public String getField1()
    {
        return field1;
    }
    public void setField1(String field1)
    {
        this.field1 = field1;
    }
    public String getField2()
    {
        return field2;
    }
    public void setField2(String field2)
    {
        this.field2 = field2;
    }
}
  
// class containing the Main method
public class GFG {
    public static void main(String[] args)
    {
  
        // get array Method objects of GFGDemoClass
        Method[] methods = GFGDemoClass.class.getMethods();
  
        // print getDeclaringclass for every Method object
        // looping through a list of method objects
        for (Method m : methods) {
  
            // get a class object which declares the current method
            // by declaringClass() method
            Class declaringClass = m.getDeclaringClass();
  
            // print Method name and class name
            System.out.println("Method Name: " + m.getName());
            System.out.println("Declaring class Name: "
                               + declaringClass.getName());
        }
    }
}
输出:
Method Name: getField1
Declaring class Name: GFGDemoClass
Method Name: setField1
Declaring class Name: GFGDemoClass
Method Name: getField2
Declaring class Name: GFGDemoClass
Method Name: setField2
Declaring class Name: GFGDemoClass
Method Name: wait
Declaring class Name: java.lang.Object
Method Name: wait
Declaring class Name: java.lang.Object
Method Name: wait
Declaring class Name: java.lang.Object
Method Name: equals
Declaring class Name: java.lang.Object
Method Name: toString
Declaring class Name: java.lang.Object
Method Name: hashCode
Declaring class Name: java.lang.Object
Method Name: getClass
Declaring class Name: java.lang.Object
Method Name: notify
Declaring class Name: java.lang.Object
Method Name: notifyAll
Declaring class Name: java.lang.Object

程序 2:通过应用 getDeclaringClass() 方法来查找 Object 类方法的详细信息。

// Program Demonstrate getDeclaringClass() method
// of Method Class.
  
import java.lang.reflect.Method;
  
// sample class containing some method
class BasicOperations {
  
    // create attributes
    int a;
    int b;
  
    // create methods
    public int add()
    {
        return a + b;
    }
  
    public int multiply()
    {
        return a * b;
    }
  
    public int subtract()
    {
        return a - b;
    }
  
    public int division()
    {
        return a / b;
    }
}
  
public class GFG {
    public static void main(String[] args)
    {
  
        // get array Method objects
        Method[] methods = BasicOperations.class.getMethods();
  
        // print getDeclaringclass for every Method object
        for (Method m : methods) {
  
            // get class object by declaringClass() method
            Class declaringClass = m.getDeclaringClass();
  
            // checks whether the method belong to
            // BasicOperations class or not
            // and if yes then print method name
            // along with declaring class name.
            if (declaringClass.getName().equals("BasicOperations")) {
  
                // print Method name and class name
                System.out.println("Method Name: " + m.getName());
                System.out.println("Declaring class Name: "
                                   + declaringClass.getName());
            }
        }
    }
}
输出:
Method Name: multiply
Declaring class Name: BasicOperations
Method Name: subtract
Declaring class Name: BasicOperations
Method Name: division
Declaring class Name: BasicOperations
Method Name: add
Declaring class Name: BasicOperations

参考: https: Java/lang/reflect/Method.html#getDeclaringClass–