该getDeclaringClass()
方法返回在其中声明该对象的类的Class对象。仅当Class对象的Class是另一个类的成员时,才会发生这种情况。否则,此方法返回null。
另外,如果Class对象表示原始类型,数组类,void等,则该getDeclaringClass()
方法返回null。
演示该getDeclaringClass()
方法的程序如下:
package Test; import java.lang.reflect.*; public class Demo { public static void main(String[] args) { Method[] m = String.class.getMethods(); for(int i = 0; i < m.length; i++) { Class decClass = m[i].getDeclaringClass(); System.out.println("\nThe Method name is: " + m[i].toString()); System.out.println("The Declaring class name is: " + decClass.toString()); } } }
输出结果
The Method name is: public boolean java.lang.String.equals(java.lang.Object) The Declaring class name is: class java.lang.String The Method name is: public java.lang.String java.lang.String.toString() The Declaring class name is: class java.lang.String The Method name is: public int java.lang.String.hashCode() The Declaring class name is: class java.lang.String The Method name is: public int java.lang.String.compareTo(java.lang.Object) The Declaring class name is: class java.lang.String The Method name is: public int java.lang.String.compareTo(java.lang.String) The Declaring class name is: class java.lang.String The Method name is: public int java.lang.String.indexOf(java.lang.String,int) The Declaring class name is: class java.lang.String The Method name is: public int java.lang.String.indexOf(int) The Declaring class name is: class java.lang.String The Method name is: public int java.lang.String.indexOf(java.lang.String) The Declaring class name is: class java.lang.String The Method name is: public int java.lang.String.indexOf(int,int) The Declaring class name is: class java.lang.String The Method name is: public static java.lang.String java.lang.String.valueOf(char) The Declaring class name is: class java.lang.String The Method name is: public static java.lang.String java.lang.String.valueOf(java.lang.Object) The Declaring class name is: class java.lang.String The Method name is: public static java.lang.String java.lang.String.valueOf(boolean) The Declaring class name is: class java.lang.String The Method name is: public static java.lang.String java.lang.String.valueOf(char[],int,int) The Declaring class name is: class java.lang.String The Method name is: public static java.lang.String java.lang.String.valueOf(char[]) The Declaring class name is: class java.lang.String The Method name is: public static java.lang.String java.lang.String.valueOf(double) The Declaring class name is: class java.lang.String The Method name is: public static java.lang.String java.lang.String.valueOf(float) The Declaring class name is: class java.lang.String The Method name is: public static java.lang.String java.lang.String.valueOf(long) The Declaring class name is: class java.lang.String The Method name is: public static java.lang.String java.lang.String.valueOf(int) The Declaring class name is: class java.lang.String The Method name is: public int java.lang.String.length() The Declaring class name is: class java.lang.String The Method name is: public boolean java.lang.String.isEmpty() The Declaring class name is: class java.lang.String The Method name is: public char java.lang.String.charAt(int) The Declaring class name is: class java.lang.String The Method name is: public int java.lang.String.codePointAt(int) The Declaring class name is: class java.lang.String The Method name is: public int java.lang.String.codePointBefore(int) The Declaring class name is: class java.lang.String The Method name is: public int java.lang.String.codePointCount(int,int) The Declaring class name is: class java.lang.String The Method name is: public int java.lang.String.offsetByCodePoints(int,int) The Declaring class name is: class java.lang.String The Method name is: public void java.lang.String.getChars(int,int,char[],int) The Declaring class name is: class java.lang.String The Method name is: public byte[] java.lang.String.getBytes() The Declaring class name is: class java.lang.String The Method name is: public byte[] java.lang.String.getBytes(java.lang.String) throws java.io.UnsupportedEncodingException The Declaring class name is: class java.lang.String The Method name is: public void java.lang.String.getBytes(int,int,byte[],int) The Declaring class name is: class java.lang.String The Method name is: public byte[] java.lang.String.getBytes(java.nio.charset.Charset) The Declaring class name is: class java.lang.String The Method name is: public boolean java.lang.String.contentEquals(java.lang.StringBuffer) The Declaring class name is: class java.lang.String The Method name is: public boolean java.lang.String.contentEquals(java.lang.CharSequence) The Declaring class name is: class java.lang.String The Method name is: public boolean java.lang.String.equalsIgnoreCase(java.lang.String) The Declaring class name is: class java.lang.String The Method name is: public int java.lang.String.compareToIgnoreCase(java.lang.String) The Declaring class name is: class java.lang.String The Method name is: public boolean java.lang.String.regionMatches(int,java.lang.String,int,int) The Declaring class name is: class java.lang.String The Method name is: public boolean java.lang.String.regionMatches(boolean,int,java.lang.String,int,int) The Declaring class name is: class java.lang.String The Method name is: public boolean java.lang.String.startsWith(java.lang.String) The Declaring class name is: class java.lang.String The Method name is: public boolean java.lang.String.startsWith(java.lang.String,int) The Declaring class name is: class java.lang.String The Method name is: public boolean java.lang.String.endsWith(java.lang.String) The Declaring class name is: class java.lang.String The Method name is: public int java.lang.String.lastIndexOf(int,int) The Declaring class name is: class java.lang.String The Method name is: public int java.lang.String.lastIndexOf(java.lang.String,int) The Declaring class name is: class java.lang.String The Method name is: public int java.lang.String.lastIndexOf(int) The Declaring class name is: class java.lang.String The Method name is: public int java.lang.String.lastIndexOf(java.lang.String) The Declaring class name is: class java.lang.String The Method name is: public java.lang.String java.lang.String.substring(int) The Declaring class name is: class java.lang.String The Method name is: public java.lang.String java.lang.String.substring(int,int) The Declaring class name is: class java.lang.String The Method name is: public java.lang.CharSequence java.lang.String.subSequence(int,int) The Declaring class name is: class java.lang.String The Method name is: public java.lang.String java.lang.String.concat(java.lang.String) The Declaring class name is: class java.lang.String The Method name is: public java.lang.String java.lang.String.replace(char,char) The Declaring class name is: class java.lang.String The Method name is: public java.lang.String java.lang.String.replace(java.lang.CharSequence,java.lang.CharSequence) The Declaring class name is: class java.lang.String The Method name is: public boolean java.lang.String.matches(java.lang.String) The Declaring class name is: class java.lang.String The Method name is: public boolean java.lang.String.contains(java.lang.CharSequence) The Declaring class name is: class java.lang.String The Method name is: public java.lang.String java.lang.String.replaceFirst(java.lang.String,java.lang.String) The Declaring class name is: class java.lang.String The Method name is: public java.lang.String java.lang.String.replaceAll(java.lang.String,java.lang.String) The Declaring class name is: class java.lang.String The Method name is: public java.lang.String[] java.lang.String.split(java.lang.String,int) The Declaring class name is: class java.lang.String The Method name is: public java.lang.String[] java.lang.String.split(java.lang.String) The Declaring class name is: class java.lang.String The Method name is: public static java.lang.String java.lang.String.join(java.lang.CharSequence,java.lang.CharSequence[]) The Declaring class name is: class java.lang.String The Method name is: public static java.lang.String java.lang.String.join(java.lang.CharSequence,java.lang.Iterable) The Declaring class name is: class java.lang.String The Method name is: public java.lang.String java.lang.String.toLowerCase(java.util.Locale) The Declaring class name is: class java.lang.String The Method name is: public java.lang.String java.lang.String.toLowerCase() The Declaring class name is: class java.lang.String The Method name is: public java.lang.String java.lang.String.toUpperCase() The Declaring class name is: class java.lang.String The Method name is: public java.lang.String java.lang.String.toUpperCase(java.util.Locale) The Declaring class name is: class java.lang.String The Method name is: public java.lang.String java.lang.String.trim() The Declaring class name is: class java.lang.String The Method name is: public char[] java.lang.String.toCharArray() The Declaring class name is: class java.lang.String The Method name is: public static java.lang.String java.lang.String.format(java.util.Locale,java.lang.String,java.lang.Object[]) The Declaring class name is: class java.lang.String The Method name is: public static java.lang.String java.lang.String.format(java.lang.String,java.lang.Object[]) The Declaring class name is: class java.lang.String The Method name is: public static java.lang.String java.lang.String.copyValueOf(char[],int,int) The Declaring class name is: class java.lang.String The Method name is: public static java.lang.String java.lang.String.copyValueOf(char[]) The Declaring class name is: class java.lang.String The Method name is: public native java.lang.String java.lang.String.intern() The Declaring class name is: class java.lang.String The Method name is: public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException The Declaring class name is: class java.lang.Object The Method name is: public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException The Declaring class name is: class java.lang.Object The Method name is: public final void java.lang.Object.wait() throws java.lang.InterruptedException The Declaring class name is: class java.lang.Object The Method name is: public final native java.lang.Class java.lang.Object.getClass() The Declaring class name is: class java.lang.Object The Method name is: public final native void java.lang.Object.notify() The Declaring class name is: class java.lang.Object The Method name is: public final native void java.lang.Object.notifyAll() The Declaring class name is: class java.lang.Object The Method name is: public default java.util.stream.IntStream java.lang.CharSequence.codePoints() The Declaring class name is: interface java.lang.CharSequence The Method name is: public default java.util.stream.IntStream java.lang.CharSequence.chars() The Declaring class name is: interface java.lang.CharSequence