java反射、注解

67 阅读1分钟

getClass方法:获取对象的类名 Class.forName():调用该访问 返回一个以字符串指定类名的类的对象。 String name = "Huanglinqing"; Class c1 = name.getClass(); System.out.println(c1.getName());

c1.getSuperclass()获取到他的父类

获取类的成员 当类中方法定义为私有的时候我们能调用?不能!当变量是私有的时候我们能获取吗?不能!但是反射可以,比如源码中有你需要用到的方法,但是那个方法是私有的,这个时候你就可以通过反射去执行这个私有方法,并且获取私有变量。

获取类的所有构造方法
Test test = new Test(); Class c4 = test.getClass(); Constructor[] constructors ; constructors = c4.getDeclaredConstructors();

注解