通过反射获取Class
方法一
package reflectiondemo;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class test {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
// 反射获取class
Class<?> aClass = Class.forName("java.lang.String");
Method[] methods = aClass.getDeclaredMethods();
for (Method method : methods){
System.out.println(method);
}
}
}
方法二
String str = "abcd";
Class<?> aclass = str.getClass();
Method[] methods = aclass.getDeclaredMethods();
for(Method method : methods){
System.out.println(method);
}
方法三
Class<?> aclass = String.class;
Method[] methods = aclass.getDeclaredMethods();
for(Method method : methods) {
System.out.println(method);
}