當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Method類 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包的超類對象繼承的。

// 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://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getDeclaringClass-




相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Method Class | getDeclaringClass() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。