描述
這個java.lang.Class.getDeclaringClass() 如果此 Class 對象表示的類或接口是另一個類的成員,則方法返回表示在其中聲明它的類的 Class 對象。
如果此類或接口不是任何其他類的成員,則此方法返回 null。如果此 Class 對象表示數組類、原始類型或 void,則此方法返回 null。
聲明
以下是聲明java.lang.Class.getDeclaringClass()方法
public Class<?> getDeclaringClass()
參數
NA
返回值
此方法返回此類的聲明類。
異常
NA
示例
下麵的例子展示了 java.lang.Class.getDeclaringClass() 方法的用法。
package com.tutorialspoint;
import java.lang.reflect.*;
public class ClassDemo {
public static void main(String[] args) {
// class object associated with class Long
Class cls = Long.class;
Method[] m = cls.getMethods();
for(int i = 0; i < m.length; i++) {
// returns te declaring class
Class dec = m[i].getDeclaringClass();
// displays all methods
System.out.println("Method = " + m[i].toString());
System.out.println(" Declaring class:" + dec.toString());
}
}
}
讓我們編譯並運行上麵的程序,這將產生以下結果——
Method = public static int java.lang.Long.numberOfLeadingZeros(long) Declaring class:class java.lang.Long Method = public static int java.lang.Long.numberOfTrailingZeros(long) Declaring class:class java.lang.Long Method = public static int java.lang.Long.bitCount(long) Declaring class:class java.lang.Long Method = public boolean java.lang.Long.equals(java.lang.Object) Declaring class:class java.lang.Long Method = public static java.lang.String java.lang.Long.toString(long,int) Declaring class:class java.lang.Long Method = public static java.lang.String java.lang.Long.toString(long) Declaring class:class java.lang.Long Method = public java.lang.String java.lang.Long.toString() Declaring class:class java.lang.Long Method = public int java.lang.Long.hashCode() Declaring class:class java.lang.Long Method = public static long java.lang.Long.reverseBytes(long) Declaring class:class java.lang.Long Method = public int java.lang.Long.compareTo(java.lang.Long) Declaring class:class java.lang.Long Method = public int java.lang.Long.compareTo(java.lang.Object) Declaring class:class java.lang.Long Method = public static java.lang.Long java.lang.Long.getLong(java.lang.String) Declaring class:class java.lang.Long Method = public static java.lang.Long java.lang.Long.getLong(java.lang.String,long) Declaring class:class java.lang.Long Method = public static java.lang.Long java.lang.Long.getLong(java.lang.String,java.lang.Long) Declaring class:class java.lang.Long Method = public byte java.lang.Long.byteValue() Declaring class:class java.lang.Long Method = public short java.lang.Long.shortValue() Declaring class:class java.lang.Long Method = public int java.lang.Long.intValue() Declaring class:class java.lang.Long Method = public long java.lang.Long.longValue() Declaring class:class java.lang.Long Method = public float java.lang.Long.floatValue() Declaring class:class java.lang.Long Method = public double java.lang.Long.doubleValue() Declaring class:class java.lang.Long Method = public static java.lang.Long java.lang.Long.valueOf(java.lang.String,int) throws java.lang.NumberFormatException Declaring class:class java.lang.Long Method = public static java.lang.Long java.lang.Long.valueOf(java.lang.String) throws java.lang.NumberFormatException Declaring class:class java.lang.Long Method = public static java.lang.Long java.lang.Long.valueOf(long) Declaring class:class java.lang.Long Method = public static java.lang.String java.lang.Long.toHexString(long) Declaring class:class java.lang.Long Method = public static java.lang.Long java.lang.Long.decode(java.lang.String) throws java.lang.NumberFormatException Declaring class:class java.lang.Long Method = public static long java.lang.Long.reverse(long) Declaring class:class java.lang.Long Method = public static java.lang.String java.lang.Long.toOctalString(long) Declaring class:class java.lang.Long Method = public static java.lang.String java.lang.Long.toBinaryString(long) Declaring class:class java.lang.Long Method = public static long java.lang.Long.highestOneBit(long) Declaring class:class java.lang.Long Method = public static long java.lang.Long.lowestOneBit(long) Declaring class:class java.lang.Long Method = public static long java.lang.Long.rotateLeft(long,int) Declaring class:class java.lang.Long Method = public static long java.lang.Long.rotateRight(long,int) Declaring class:class java.lang.Long Method = public static int java.lang.Long.signum(long) Declaring class:class java.lang.Long Method = public static long java.lang.Long.parseLong(java.lang.String,int) throws java.lang.NumberFormatException Declaring class:class java.lang.Long Method = public static long java.lang.Long.parseLong(java.lang.String) throws java.lang.NumberFormatException Declaring class:class java.lang.Long Method = public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException Declaring class:class java.lang.Object Method = public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException Declaring class:class java.lang.Object Method = public final void java.lang.Object.wait() throws java.lang.InterruptedException Declaring class:class java.lang.Object Method = public final native java.lang.Class java.lang.Object.getClass() Declaring class:class java.lang.Object Method = public final native void java.lang.Object.notify() Declaring class:class java.lang.Object Method = public final native void java.lang.Object.notifyAll() Declaring class:class java.lang.Object
相關用法
- Java Java.lang.Class.getDeclaredMethods()用法及代碼示例
- Java Java.lang.Class.getDeclaredConstructors()用法及代碼示例
- Java Java.lang.Class.getDeclaredMethod()用法及代碼示例
- Java Java.lang.Class.getDeclaredFields()用法及代碼示例
- Java Java.lang.Class.getDeclaredConstructor()用法及代碼示例
- Java Java.lang.Class.getDeclaredClasses()用法及代碼示例
- Java Java.lang.Class.getDeclaredField()用法及代碼示例
- Java Java.lang.Class.getResource()用法及代碼示例
- Java Java.lang.Class.getConstructor()用法及代碼示例
- Java Java.lang.Class.getConstructors()用法及代碼示例
- Java Java.lang.Class.getMethod()用法及代碼示例
- Java Java.lang.Class.getInterfaces()用法及代碼示例
- Java Java.lang.Class.getModifiers()用法及代碼示例
- Java Java.lang.Class.getName()用法及代碼示例
- Java Java.lang.Class.getTypeParameters()用法及代碼示例
- Java Java.lang.Class.getClasses()用法及代碼示例
- Java Java.lang.Class.getEnclosingMethod()用法及代碼示例
- Java Java.lang.Class.getResourceAsStream()用法及代碼示例
- Java Java.lang.Class.getComponentType()用法及代碼示例
- Java Java.lang.Class.getGenericSuperClass()用法及代碼示例
注:本文由純淨天空篩選整理自 Java.lang.Class.getDeclaringClass() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。