反射机制

反射机制

反射机制是什么

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();
getDeclaredFields 和 getFields 的区别
  • getDeclaredFields()获得某个类的所有申明的字段,即包括public、private和proteced,但是不包括父类的申明字段。
  • getFields()获得某个类的所有的公共(public)的字段,包括父类。

获取类中的信息

  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);
    	}
    
    }
    







  • 62
    点赞
  • 199
    收藏
    觉得还不错? 一键收藏
  • 19
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值