getDeclaringClass()
方法getDeclaringClass()方法在java.lang包中可用。
getDeclaringClass()方法用于返回声明的Class对象,该对象表示在此Class表示的该类或接口是其他类的一部分或成员时在其中进行声明的Class。
getDeclaringClass()方法是一个非静态方法,仅可通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
返回Class对象时,getDeclaringClass()方法不会引发异常。
语法:
public Class getDeclaringClass ();
参数:
它不接受任何参数。
返回值:
此方法的返回类型为Class,它从该Class返回一个声明的class的对象。
注意:
当该类或接口不是其他类的一部分或成员时,将返回null。
当此类反映原始类型或void类型或数组类时,将返回null。
示例
//Java程序演示示例 //类的ClassgetDeclaringClass()方法 import java.lang.reflect.*; public class GetDeclaringClassOfClass { public static void main(String[] args) { //返回Class对象 //给定的类名 Class cl = Integer.class; //它返回一个方法数组 //表示私有,受保护,公共和默认 //类Integer- Method[] methods = cl.getMethods(); //遍历整数类 for (int i = 0; i < methods.length; ++i) { Class declare_classes = methods[i].getDeclaringClass(); System.out.print(" Declaring Methods Class: "); System.out.println(declare_classes.toString()); } } }
输出结果
Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Integer Declaring Methods Class: class java.lang.Object Declaring Methods Class: class java.lang.Object Declaring Methods Class: class java.lang.Object Declaring Methods Class: class java.lang.Object Declaring Methods Class: class java.lang.Object Declaring Methods Class: class java.lang.Object