public class ReflectionExample { public static void main(String[] args) { try { Class<?> clazz = Class.forName("com.example.MyClass"); Field[] fields = clazz.getDeclaredFields(); Method[] methods = clazz.getDeclaredMethods(); for (Field field : fields) { System.out.println("Field: " + field.getName()); } for (Method method : methods) { System.out.println("Method: " + method.getName()); } Object obj = clazz.newInstance(); System.out.println("Instance created: " + obj.getClass().getName()); e.printStackTrace(); } } } class MyClass { public int myField; private String myMethod() { return "Hello, World!"; } }


上一篇:
下一篇:
切换中文