java.lang.Class.getDeclaringClass()方法範例


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.yiibai;

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