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-
相关用法
- Java Class getDeclaringClass()用法及代码示例
- Java Constructor getDeclaringClass()用法及代码示例
- Java Field getDeclaringClass()用法及代码示例
- Java Method类 isBridge()用法及代码示例
- Java Method类 getReturnType()用法及代码示例
- Java Method类 getTypeParameters()用法及代码示例
- Java Method类 getGenericParameterTypes()用法及代码示例
- Java Method类 toGenericString()用法及代码示例
- Java Method类 getParameterTypes()用法及代码示例
- Java Method类 getGenericExceptionTypes()用法及代码示例
- Java Method类 getExceptionTypes()用法及代码示例
- Java Method类 hashCode()用法及代码示例
- Java Method类 getAnnotatedReturnType()用法及代码示例
- Java Method类 getParameterAnnotations()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Method Class | getDeclaringClass() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。