代码示例:
public class Solution { public static void main(String[] args) { // 自定义类,使用的系统类加载器进行加载 ClassLoader classLoader = Solution.class.getClassLoader(); System.out.println(classLoader); // 调用系统类加载器的getParent():拓展类加载器 ClassLoader classLoader1 = classLoader.getParent(); System.out.println(classLoader1); // 调用拓展类加载器的getParent():无法获取引导类加载器 // 引导类加载器主要负责Java的核心库,无法加载自定义类 ClassLoader classLoader2 = classLoader1.getParent(); System.out.println(classLoader2); // 使用的引导类加载器 但是无法获取到 ClassLoader classLoader3 = String.class.getClassLoader(); System.out.println(classLoader3); } }控制台打印结果:
反射的基本操作 获取运行时类的结构 获取属性获取当前运行时类及其父类中声明为public访问权限的属性
public class Solution { public static void main(String[] args) { Class<Solution> solutionClass = Solution.class; Field[] fields = solutionClass.getFields(); } }获取当前运行时类自己声明的所有属性,不包含父类中声明的属性
public class Solution { public static void main(String[] args) { Class<Solution> solutionClass = Solution.class; Field[] declaredFields = solutionClass.getDeclaredFields(); } }获取属性中的某一个结构部分
public class Solution { public static void main(String[] args) { Class<Solution> solutionClass = Solution.class; Field[] declaredFields = solutionClass.getDeclaredFields(); for (Field declaredField : declaredFields) { // 获取权限修饰符 int modifiers = declaredField.getModifiers(); System.out.println(Modifier.toString(modifiers)); // 获取数据类型 Class<?> type = declaredField.getType(); System.out.println(type); // 获取变量名 String name = declaredField.getName(); System.out.println(name); } } } 获取方法获取当前运行时类及其父类的中声明为public权限的方法
public class Solution { public static void main(String[] args) { Class<Solution> solutionClass = Solution.class; Method[] methods = solutionClass.getMethods(); } }获取当前运行时类中声明的所有方法(不包含父类中声明的)
public class Solution { public static void main(String[] args) { Class<Solution> solutionClass = Solution.class; Method[] methods = solutionClass.getDeclaredMethods(); } }获取方法中的某一个结构部分
public class Solution { public static void main(String[] args) { Class<Solution> solutionClass = Solution.class; Method[] methods = solutionClass.getDeclaredMethods(); for (Method method : methods) { // 获取方法声明的注解 Annotation[] annotations = method.getAnnotations(); // 获取权限修饰符 int modifiers = method.getModifiers(); System.out.println(Modifier.toString(modifiers)); // 获取返回值类型 String returnType = method.getReturnType().getName(); // 获取方法名 String methodName = method.getName(); // 获取形参列表 Class<?>[] parameterTypes = method.getParameterTypes(); // 获取抛出的异常 Class<?>[] exceptionTypes = method.getExceptionTypes(); } } } 获取构造器获取当前运行时类中声明为public的构造器
public class Solution { public static void main(String[] args) { Class<Solution> solutionClass = Solution.class; Constructor<?>[] constructors = solutionClass.getConstructors(); } }获取当前运行时类中声明的所有构造器
public class Solution { public static void main(String[] args) { Class<Solution> solutionClass = Solution.class; Constructor<?>[] constructors = solutionClass.getDeclaredConstructors(); } } 获取父类获取运行时类的父类
public class Solution { public static void main(String[] args) { Class<Solution> solutionClass = Solution.class; Class<? super Solution> superclass = solutionClass.getSuperclass(); } }