1.反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;
2.对于任意一个对象,都能够调用它的任意一个方法和属性;
3.这种动态获取的信息以及动态调用对象的方法的功能就是反射机制;
4.也就是说通过反射机制,我们可以获取想要获取到的东西,对前面所学到的范围限定词的限制就可以打破约束
1.在运行时判断任意一个对象所属的类;
2.在运行时构造任意一个类的对象;
3.在运行时判断任意一个类所具有的成员变量和方法;
4.在运行时调用任意一个对象的方法;
5.生成动态代理;
由于反射机制操作的是类的字节码文件对象,所以想要使用反射机制就必须获取到该类的字节码文件,获取类的字节码文件有3种操作方式
1.通过Class类中的静态方法forName();可以获取到字节码对象
Class class1 = class.forName("包名.类名")
2.直接用该类.class获取字节码文件对象
Class class2 = Dog.class;
3.通过类的实例获取该类的字节码文件对象
Class class3 = new Dog().getClass();
获取类中的信息
import java.lang.reflect.Field;
class Person extends Human{
public int age;
private String name;
protected long id;
}
class Human{
public int hight;
}
public class Main{
public static void main(String[] args) {
Class class1 = Person.class;
Field[] f1 = class1.getFields();//获取到所有被public修饰的类信息,包括父类中的
for(Field f:f1)
System.out.println(f);
Field[] f2 = class1.getDeclaredFields();//获取到本类中所有类的信息
for(Field f:f2)
System.out.println(f);
}
}
获取指定的类属性对象
import java.lang.reflect.Field;
class Person extends Human{
public int age;
private String name;
protected long id;
}
class Human{
public int hight;
}
public class Main{
//因为存在访问不到的可能,所以需要抛出异常
public static void main(String[] args)throws Exception {
Class class1 = Person.class;
Field f1 = class1.getField("age");//只能获取父类及本类被public修饰的类属性
Field f2 = class1.getDeclaredField("name");//可以任意获取本类中的指定类属性
System.out.println(f1);
System.out.println(f2);
}
}
获取构造方法信息
import java.lang.reflect.Constructor;
class Person extends Human {
public int age;
private String name;
protected long id;
public Person(int age, String name, long id) {
this.age = age;
this.name = name;
this.id = id;
}
public Person() {
}
private Person(int age, String name) {
this.age = age;
this.name = name;
}
}
class Human {
public int hight;
}
public class Main {
public static void main(String[] args) throws Exception {
Class class1 = Person.class;
Constructor[] c1 = class1.getConstructors();//获取所有被public修饰的构造器
for (Constructor c : c1)
System.out.println(c);
System.out.println("--------------");
Constructor[] c2 = class1.getDeclaredConstructors();//获取本类所有的构造器
for (Constructor c : c2)
System.out.println(c);
}
}
获取指定的构造方法
import java.lang.reflect.Constructor;
class Person extends Human {
public int age;
private String name;
protected long id;
public Person(int age, String name, long id) {
this.age = age;
this.name = name;
this.id = id;
}
public Person() {
}
public Person(int age) {
}
private Person(int age, String name) {
this.age = age;
this.name = name;
}
}
class Human {
public int hight;
}
public class Main {
public static void main(String[] args) throws Exception {
Class class1 = Person.class;
Constructor c1 = class1.getConstructor(null);//无参构造器
System.out.println(c1);
Constructor c2 = class1.getConstructor(int.class);//有参构造器
System.out.println(c2);
System.out.println("--------------");
Constructor c3 = class1.getDeclaredConstructor(int.class,String.class);
System.out.println(c3);
}
}
获取方法的信息
import java.lang.reflect.Method;
class Person extends Human {
public int age;
private String name;
protected long id;
public Person(int age, String name, long id) {
this.age = age;
this.name = name;
this.id = id;
}
public Person() {
}
public Person(int age) {
}
private Person(int age, String name) {
this.age = age;
this.name = name;
}
public void aa() {
}
private void bb() {
}
private void cc() {
}
}
class Human {
public int hight;
}
public class Main {
public static void main(String[] args) throws Exception {
Class class1 = Person.class;
//获取到本类被public修饰以及父类中被public修饰的方法
Method[] m1 = class1.getMethods();
for(Method m : m1)
System.out.println(m);
System.out.println("------------");
Method[] m2 = class1.getDeclaredMethods();//获取到本类中所有的方法
for(Method m : m2)
System.out.println(m);
}
}
获取指定方法的信息
import java.lang.reflect.Method;
class Person extends Human {
public int age;
private String name;
protected long id;
public Person(int age, String name, long id) {
this.age = age;
this.name = name;
this.id = id;
}
public Person() {
}
public Person(int age) {
}
private Person(int age, String name) {
this.age = age;
this.name = name;
}
public void aa() {
}
private void bb(int x) {
System.out.println("bb方法");
}
private void cc() {
}
}
class Human {
public int hight;
public void oo() {
}
}
public class Main {
public static void main(String[] args) throws Exception {
Class class1 = Person.class;
Method m1 = class1.getMethod("aa");//只能获取本类及父类中被public修饰的方法
System.out.println(m1);
//可以获取本类中所有任意指定的方法
Method m2 = class1.getDeclaredMethod("bb", int.class);
System.out.println(m2);
}
}
对反射机制的使用
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
class Person extends Human {
public int age;
private String name;
protected long id;
public Person(int age, String name, long id) {
this.age = age;
this.name = name;
this.id = id;
}
public Person() {
}
public Person(int age) {
}
private Person(int age, String name) {
this.age = age;
this.name = name;
}
public void aa() {
}
private void bb(int x) {
System.out.println("bb方法");
}
private void cc() {
}
public static void dd() {
}
}
class Human {
public int hight;
void oo() {
}
}
public class Main {
public static void main(String[] args) throws Exception {
Class class1 = Person.class;
Object obj = class1.newInstance();
//使用无参的构造方法创建对象
System.out.println(obj);
Constructor constructor1 =
class1.getDeclaredConstructor(int.class,String.class);
constructor1.setAccessible(true);
Object dog = constructor1.newInstance(10,"aa");
System.out.println(dog);
Field name = class1.getDeclaredField("name");
name.setAccessible(true);
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);
Method dd = class1.getDeclaredMethod("dd");
dd.invoke(null);
System.out.println(dd);
}
}