import java.lang.reflect.Method;
public class ReflectionExample {
public static void main(String[] args) {
try {
// 类的全限定名
String className = "java.util.ArrayList";
// 通过反射获取类
Class<?> clazz = Class.forName(className);
// 获取所有public方法
Method[] methods = clazz.getMethods();
// 遍历并打印方法名和参数列表
for (Method method : methods) {
StringBuilder paramList = new StringBuilder();
for (Class<?> paramType : method.getParameterTypes()) {
paramList.append(paramType.getSimpleName()).append(", ");
}
if (paramList.length() > 0) {
paramList.setLength(paramList.length() - 2);
}
System.out.println(method.getName() + "(" + paramList.toString() + ")");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}