反射
1.获取Class对象的方法
public class Test {
public static void main(String[] args) {
Class clazz1 = Student.class;
Student s = new Student(18, 0);
Class clazz2 = s.getClass();
try {
Class clazz3 = Class.forName("cn.itlaobing.ref.Student");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
2.获取构造器
方法 | 描述 |
---|
Connstructor getConstructor(Class<?>…paramterTypes) | 获取Class对象对应类的带指定参数列表的public构造器,如果参数列表中参数的class对象和构造方中参数列表类型不匹配或者构造方法不是public修饰的,则抛出NoSuchMethodException |
Connstructor<?> getContructors() | 获取Class对象对应类的所有public构造器 |
Connstructor getDeclaredConstructor (Class<?>…paramterTypes) | 获取Class对象对应类的带指定参数列表的构造器,与访问修饰符无关 |
Connstructor<?> getDeclaredConstructors() | 获取Class对象对应类的所有构造器,与访问修饰符无关 |
2.1示例
public class Test2 {
public static void main(String[] args) throws NoSuchMethodException, SecurityException {
Class<Student> clazz = Student.class;
Constructor[] c1 = clazz.getConstructors();
Constructor[] c2 = clazz.getDeclaredConstructors();
Constructor c = clazz.getConstructor(int.class,double.class);
System.out.println(c);
Constructor construct1 = clazz.getDeclaredConstructor(double.class);
System.out.println(construct1);
Constructor construct2 = clazz.getDeclaredConstructor();
System.out.println(construct2);
}
}
3.获取成员方法
方法 | 描述 |
---|
Method getMethod(String name,Class<?>…paramterTypes) | 获取指定名称和参数类型class对象的public修饰方法,包括从父继承的public修饰的方法 |
Method[] getMethods() | 获取所有public修饰的成员方法以及继承的方法,包括类方法 |
Method getDeclaredMethod(String name,Class<?>…paramterTypes) | 获取指定名称和参数类型class对象的方法,与访问修饰符无关 |
Method[] getDeclaredMethods() | 获取自身声明的所有成员方法,与访问修饰符无关 |
3.1示例
public class MethodTest {
public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Student s = new Student(0);
Class clazz = s.getClass();
Method[] methods = clazz.getMethods();
Method[] methods2 = clazz.getDeclaredMethods();
Method method2 = clazz.getDeclaredMethod("newObject", int.class);
Method method3 = clazz.getDeclaredMethod("makeMoney");
method3.invoke(s);
Method method4 = clazz.getDeclaredMethod("sleep",String.class);
method4.invoke(null,"蔡老师");
}
}
4.获取成员变量
方法 | 描述 |
---|
Field getField(String name) | 获取指定名称的public修饰的成员变量 |
Field[] getFields() | 获取所有的public修饰的成员变量,包括从父类继承的成员变量 |
Field getDeclaredField(String name) | 获取指定名称的成员变量,与访问权限无关 |
Field[] getDeclaredFields() | 获取所有的成员变量,与访问修饰符无关 |
4.1示例
public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
Son s = new Son();
Class clazz = s.getClass();
Field[] fields = clazz.getFields();
Field[] fields2 = clazz.getDeclaredFields();
Field f1 = clazz.getField("age");
f1.setInt(s, 10);
System.out.println(s.age);
Field f3 = clazz.getField("name");
f3.set(s, "张三");
System.out.println(s.name);
Field f2 = clazz.getDeclaredField("width");
System.out.println(f2);
f2.setAccessible(true);
f2.setInt(s, 100);
System.out.println(s.getWidth());
}
5.获取注解
6.Method对象方法
方法 | 描述 |
---|
invoke(Object obj,Object…obj) | 调用对象方法 |
setAccessible(boolean flag) | 设置私有方法可访问 |
7.实例化对象
方法 | 描述 |
---|
clazz.newInstance() | 只能调用默认无参构造方法 |
Constructor.newInstance(Object…obj) | 调用对应的构造方法 |
7.1示例
public class NewInstance {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class clazz = Class.forName("cn.itlaobing.ref2.Student");
Constructor c = clazz.getDeclaredConstructor(int.class);
c.setAccessible(true);
Student s = (Student)c.newInstance(18);
System.out.println(s.getAge());
}
}