1. Get the parent class
This is the simplest and easiest way to understand--getsuperclass (), the only thing to note is that the returned must be a class, not an interface. 2. Get Inner class
There are two types of internal classes, class.getclasses () and class.getdeclaredclasses (), but they differ greatly from two, as shown in the following table:
Method Name |
return Results |
include interface |
include inheritance results |
contains only public |
Class.getclasses () |
Gets the inner class of this class |
Is |
Is |
Is |
Class.getdeclaredclasses () |
Gets the inner class of this class |
Is |
Whether |
Whether |
Also need to add:
1. The inner class can be either a static internal class or an instance inner class;
2. Use the Getdeclaredclasses method if you want to get all of the inner classes and do not include inherited inner classes;
3. If you want to obtain all the public classes, do not include protected, private, friendly, please use the Getclasses method;
4. Internal classes can be class, interface, enum (do not ask why there is no annotation);
5. Cannot obtain anonymous class; 3. Get wrapper class from inner class
This is the inverse method of getting the inner class, the inner class to get the wrapper class (The outer Class), the method is Class.getdeclaringclass (), it should be pointed out that because the inner class can also be declared in the interface, it is possible to return the interface. 4. Anonymous class wrapper class
The instance object allows you to get the wrapper class (external Class) where the anonymous class resides, and the method is Getenclosingclass (), as the following example:
public class Reflectclasstest {public
static void Main (string[] args) {
thread th = new Thread () {
};
Class<?> clazz = Th.getclass (). Getenclosingclass ();
The result is reflectclasstest
System.out.println (clazz);
}
Whether an anonymous class is inside a method or a property declaration, Getenclosingclass () can accurately find the wrapper class. 5. Other
Method Name |
meaning |
Java.lang.reflect.Field.getDeclaringClass () |
Gets the Run-time assignment (non-declaration) class of a property |
Java.lang.reflect.Method.getDeclaringClass () |
Gets the declaration return class of a method |
Java.lang.reflect.Constructor.getDeclaringClass () |
To get a class from a constructor |
Notice the difference between the Run-time assignment and the declaring class. 5. Reference Documentation
[1] Retrieving Class Objects