反射机制
反射机制:
在运行时,我们可以动态的获取类中的信息;在运行期间可以实例化对象,调用方法属性等等。
1.反射机制直接直接操作字节码文件,获取字节码对象
下面展示一些 内联代码片
。
class DogF {
public String hhh;
protected int xxx;
}
class Dog extends DogF{
private String name;
public Integer age;
protected int height;
public static double aa;
public Dog() {
}
private Dog(String name) {
}
public void aa() {
System.out.println("aa方法");
}
private void bb(int x) {
System.out.println("x = " + x);
}
public static void ss() {
}
}
class DogF {
public String hhh;
protected int xxx;
}
class Dog extends DogF{
private String name;
public Integer age;
protected int height;
public static double aa;
public Dog() {
}
private Dog(String name) {
}
public void aa() {
System.out.println("aa方法");
}
private void bb(int x) {
System.out.println("x = " + x);
}
public static void ss() {
}
}
反射机制:在运行时,我们可以动态的获取类中的信息; 在运行期间可以实例化对象、调用方法、属性等等。
下面展示一些 内联代码片
。
public class MyReflect {
public static void main(String[] args) throws ClassNotFoundException,
NoSuchFieldException, SecurityException, NoSuchMethodException {
1.反射机制直接操作的字节码文件,获取字节码对象
Class class1 = Dog.class;
Class class2 = Class.forName("yandaCollection1.Dog");
Class class3 = new Dog().getClass();
//获取类中的信息
Class class1 = Dog.class;
//获取类中定义的属性
//只能获取被public修饰的,还有从父类继承而来的
Field[] fields = class1.getFields();
//本类中定义的所有的属性
Field[] fields = class1.getDeclaredFields();
for (Field field : fields) {
System.out.println(field);
}
//获取指定的属性对象
Field ageF = class1.getField("name");
Field ageF = class1.getDeclaredField("name");
System.out.println(ageF);
//获取构造方法信息
Constructor[] constructors =
class1.getConstructors();
Constructor[] constructors =
class1.getDeclaredConstructors();
for (Constructor constructor : constructors) {
System.out.println(constructor);
}
//获取指定的构造方法
Constructor constructor =
class1.getConstructor();
System.out.println(constructor);
Constructor constructor2 =
class1.getDeclaredConstructor(String.class);
System.out.println(constructor2);
//获取方法的信息
Method[] methods = class1.getMethods();
Method[] methods = class1.getDeclaredMethods();
for (Method method : methods) {
System.out.println(method);
}
Method method = class1.getMethod("aa");
System.out.println(method);
Method method2 = class1.getDeclaredMethod("bb", int.class);
System.out.println(method2);
}
反射机制使用
public static void main(String[] args) throws InstantiationException,
IllegalAccessException, NoSuchMethodException, SecurityException,
IllegalArgumentException, InvocationTargetException,
NoSuchFieldException {
Class class1 = Dog.class;
//使用无参的构造方法创建对象
Object obj = class1.newInstance();
System.out.println(obj);
//使用构造方法对象 创建 当前实例
Constructor constructor =
class1.getDeclaredConstructor(String.class);
new Dog("aa");
使用非public的构造方法,需要授权
constructor.setAccessible(true);
Object dog = constructor.newInstance("aa");
System.out.println(dog);
Field name = class1.getDeclaredField("name");
name.setAccessible(true);
//为dog对象的name属性赋值为张三
//如果是静态变量,第一参数设置为null
name.set(dog, "张三");
//获取dog对象的name属性的值
System.out.println(name.get(dog));
Method aa = class1.getMethod("aa");
//调用了dog对象的aa方法
aa.invoke(dog);
Method bb = class1.getDeclaredMethod("bb", int.class);
bb.setAccessible(true);
//静态方法将第一个参数设置为null
bb.invoke(dog, 10);
}
}