Java Class getDeclaringClass() 方法



描述

Java Class getDeclaringClass() 方法返回表示声明它的类的 Class 对象,如果此 Class 对象表示的类或接口是另一个类的成员。

如果此类或接口不是任何其他类的成员,则此方法返回 null。如果此 Class 对象表示数组类、基本类型或 void,则此方法返回 null。

声明

以下是java.lang.Class.getDeclaringClass() 方法的声明

public Class<?> getDeclaringClass()

参数

返回值

此方法返回此类的声明类。

异常

获取类的声明类示例

以下示例演示了 java.lang.Class.getDeclaringClass() 方法的用法。在这个程序中,我们创建了一个 ClassDemo 的实例,然后使用 getClass() 方法检索实例的类。使用 getDeclaringClass(),我们检索了声明的类并打印它们。

package com.tutorialspoint;

import java.lang.reflect.Method;

public class ClassDemo {

   public static void main(String[] args) {
           
      ClassDemo c = new ClassDemo();
      Class cls = c.getClass();
       
      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());
      }
   }

   public ClassDemo() {
      // no argument constructor
   }

   public ClassDemo(long l, int i) {
      this.l = l;
      this.i = i;
   }

   long l = 77688;
   int i = 3;
}

输出

让我们编译并运行上述程序,这将产生以下结果:

Method = public static void com.tutorialspoint.ClassDemo.main(java.lang.String[])
 Declaring class: class com.tutorialspoint.ClassDemo
Method = public boolean java.lang.Object.equals(java.lang.Object)
 Declaring class: class java.lang.Object
Method = public java.lang.String java.lang.Object.toString()
 Declaring class: class java.lang.Object
Method = public native int java.lang.Object.hashCode()
 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
Method = public final 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

获取 Long 类的声明类示例

以下示例演示了 java.lang.Class.getDeclaringClass() 方法的用法。在这个程序中,我们检索了 Long 类的类。使用 getDeclaringClass(),我们检索了所需的类并打印它。

package com.tutorialspoint;

import java.lang.reflect.Method;

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

获取 ArrayList 类的声明类示例

以下示例演示了 java.lang.Class.getDeclaringClass() 方法的用法。在这个程序中,我们检索了 ArrayList 类的类。使用 getDeclaringClass(),我们检索了所需的类并打印它。

package com.tutorialspoint;

import java.lang.reflect.Method;
import java.util.ArrayList;


public class ClassDemo {

   public static void main(String[] args) {

      // class object associated with class ArrayList
      Class cls = ArrayList.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 boolean java.util.ArrayList.remove(java.lang.Object)
 Declaring class: class java.util.ArrayList
Method = public java.lang.Object java.util.ArrayList.remove(int)
 Declaring class: class java.util.ArrayList
Method = public int java.util.ArrayList.size()
 Declaring class: class java.util.ArrayList
Method = public java.lang.Object java.util.ArrayList.get(int)
 Declaring class: class java.util.ArrayList
Method = public boolean java.util.ArrayList.equals(java.lang.Object)
 Declaring class: class java.util.ArrayList
Method = public int java.util.ArrayList.hashCode()
 Declaring class: class java.util.ArrayList
Method = public java.lang.Object java.util.ArrayList.clone()
 Declaring class: class java.util.ArrayList
Method = public int java.util.ArrayList.indexOf(java.lang.Object)
 Declaring class: class java.util.ArrayList
Method = public void java.util.ArrayList.clear()
 Declaring class: class java.util.ArrayList
Method = public int java.util.ArrayList.lastIndexOf(java.lang.Object)
 Declaring class: class java.util.ArrayList
Method = public boolean java.util.ArrayList.isEmpty()
 Declaring class: class java.util.ArrayList
Method = public void java.util.ArrayList.replaceAll(java.util.function.UnaryOperator)
 Declaring class: class java.util.ArrayList
Method = public void java.util.ArrayList.add(int,java.lang.Object)
 Declaring class: class java.util.ArrayList
Method = public boolean java.util.ArrayList.add(java.lang.Object)
 Declaring class: class java.util.ArrayList
Method = public java.util.List java.util.ArrayList.subList(int,int)
 Declaring class: class java.util.ArrayList
Method = public java.lang.Object[] java.util.ArrayList.toArray()
 Declaring class: class java.util.ArrayList
Method = public java.lang.Object[] java.util.ArrayList.toArray(java.lang.Object[])
 Declaring class: class java.util.ArrayList
Method = public java.util.Iterator java.util.ArrayList.iterator()
 Declaring class: class java.util.ArrayList
Method = public boolean java.util.ArrayList.contains(java.lang.Object)
 Declaring class: class java.util.ArrayList
Method = public java.util.Spliterator java.util.ArrayList.spliterator()
 Declaring class: class java.util.ArrayList
Method = public boolean java.util.ArrayList.addAll(int,java.util.Collection)
 Declaring class: class java.util.ArrayList
Method = public boolean java.util.ArrayList.addAll(java.util.Collection)
 Declaring class: class java.util.ArrayList
Method = public java.lang.Object java.util.ArrayList.set(int,java.lang.Object)
 Declaring class: class java.util.ArrayList
Method = public void java.util.ArrayList.forEach(java.util.function.Consumer)
 Declaring class: class java.util.ArrayList
Method = public void java.util.ArrayList.sort(java.util.Comparator)
 Declaring class: class java.util.ArrayList
Method = public void java.util.ArrayList.ensureCapacity(int)
 Declaring class: class java.util.ArrayList
Method = public void java.util.ArrayList.trimToSize()
 Declaring class: class java.util.ArrayList
Method = public boolean java.util.ArrayList.removeIf(java.util.function.Predicate)
 Declaring class: class java.util.ArrayList
Method = public java.lang.Object java.util.ArrayList.getFirst()
 Declaring class: class java.util.ArrayList
Method = public java.lang.Object java.util.ArrayList.getLast()
 Declaring class: class java.util.ArrayList
Method = public void java.util.ArrayList.addFirst(java.lang.Object)
 Declaring class: class java.util.ArrayList
Method = public void java.util.ArrayList.addLast(java.lang.Object)
 Declaring class: class java.util.ArrayList
Method = public java.lang.Object java.util.ArrayList.removeFirst()
 Declaring class: class java.util.ArrayList
Method = public java.lang.Object java.util.ArrayList.removeLast()
 Declaring class: class java.util.ArrayList
Method = public boolean java.util.ArrayList.removeAll(java.util.Collection)
 Declaring class: class java.util.ArrayList
Method = public boolean java.util.ArrayList.retainAll(java.util.Collection)
 Declaring class: class java.util.ArrayList
Method = public java.util.ListIterator java.util.ArrayList.listIterator(int)
 Declaring class: class java.util.ArrayList
Method = public java.util.ListIterator java.util.ArrayList.listIterator()
 Declaring class: class java.util.ArrayList
Method = public java.lang.String java.util.AbstractCollection.toString()
 Declaring class: class java.util.AbstractCollection
Method = public boolean java.util.AbstractCollection.containsAll(java.util.Collection)
 Declaring class: class java.util.AbstractCollection
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
Method = public final 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 default java.lang.Object[] java.util.Collection.toArray(java.util.function.IntFunction)
 Declaring class: interface java.util.Collection
Method = public default java.util.stream.Stream java.util.Collection.stream()
 Declaring class: interface java.util.Collection
Method = public default java.util.stream.Stream java.util.Collection.parallelStream()
 Declaring class: interface java.util.Collection
Method = public default java.util.SequencedCollection java.util.List.reversed()
 Declaring class: interface java.util.List
Method = public default java.util.List java.util.List.reversed()
 Declaring class: interface java.util.List
java_lang_class.htm
广告