反射

1.什么是反射

反射是一种机制,利用反射机制动态的实例化对象、读写属性、调用方法、构造函数。

2.如何得到类对象

  1. 一切反射相关的代码都从获取的类对象开始。
  2. 三种方式
    ①.类名.Class
    ②.对象名.getName
    ③.Class.forName(全限定名/全路径名)

3.根据类得到类名(全限定名/全路径名)

	1)cName.getName();						-->获取全限定名吗
	2)cName.getSimpleName(); 				-->获取类名
	3)cName.getPackage(); 					-->获取包名

4.根据类得到类的属性

	//方法
		Field field=cla.getField(“属性名”);

	field.getName();					-->获取属性名			
	filed.getType();					-->获取属性类型		
	field.getModifiers();				-->获取属性访问修饰符	
	field.set(Object,Object); 			-->设置属性值,参数1:要设置属性所在对象;参数2:要设置的值;	
	field.get(Object);					-->获取属性值,参数:要获取属性值的对象				
	field.getDeclaredField(“属性名”);	-->获取单个属性(私有、公有、受保护、默认、静态)	
	field.getDeclaredFields();			-->获取所有属性(私有、公有、受保护、默认、静态)

5.根据类得到类的方法

无参无返回、无参有参会、有参无返回、有参有返回

	cla.getMethod();				-->获取单个公有方法
	cla.getDeclaredMethod();		-->获取当个方法(包括私有、受保护、默认、公有)
	cla.getMethods();				-->获取所有公有方法
	cla.getDeclaredMethods();		-->获取所有的方法(包括私有、受保护、默认、公有)

6.根据类得到类的构造方法

	cla.getConstrutor();				-->获取单个公有构造方法
	cla.getDeclaredConstrutor();		-->获取单个构造方法(包括私有、受保护、默认、公有)
	cla.getConstrutors();				-->获取所有的公有构造方法
	cla.getDeclaredConstrutors();		-->获取所有的构造方法(包括私有、受保护、默认、公有)

7. 根据类得到类的实现接口列表

Class[] interface=cla.getInterfaces();	-->获取类对象中所有实现接口列表

如若不懂参考以下代码

public class Demo {
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
		//3种方法 . 
		//1.1类名.class
		//1.2对象名.getClass()
		//1.3Class.forName(全路径名/全限定名)
		
		//方法1
		//Class clas = Student.class;
		
		//方法2
		//Student stu = new Student();
		//Class clas = stu.getClass();
		
		//传统的方式去实例化对象
		//Student stu = new Student();
		
		//反射机制实例化对象
		Class clas = Class.forName("com.zking.reflect.entity.Student");
		Student stu =  (Student) clas.newInstance();
		
		//ClassNotFoundException:找不到这个类
		//NoSuchFieldException:没有匹配属性
		//NoSuchMethodException:没有匹配的方法
		
		/*System.out.println("类的全路径名:"+clas.getName());
		System.out.println("类的简单类名:"+clas.getSimpleName());
		System.out.println("类的包名:"+clas.getPackage());*/
		
		//1.获取类的属性(公共的,私有的,受保护的,最终的等等)
		//1.1 获取类的单个属性(公共的)
		//Field field = clas.getField("age");
		//1.2获取类的属性(公共的,私有的,受保护的,最终的等等)
		/*Field field = clas.getDeclaredField("sname");
		
		System.out.println("获取属性名:"+field.getName());
		System.out.println("获取属性类型:"+field.getType());
		System.out.println("获取属性访问修饰符:"+field.getModifiers());
		*/
		//1.3赋值和取值
		//第一个:类的对象
		//第二个:所要赋值的值
		//*注意:要设置访问权限在私有化的时候
		/*field.setAccessible(true);
		field.set(stu, "图图");
		
		//取值
		Object obj = field.get(stu);
		System.out.println(obj);*/
		
		//1.4获取类中的所有属性
		//Field[] fields = clas.getFields();
		//反射
		/*Field[] fields = clas.getDeclaredFields();
		for (Field field : fields) {
			System.out.println("获取属性名:"+field.getName());
			System.out.println("获取属性类型:"+field.getType());
			System.out.println("获取属性访问修饰符:"+field.getModifiers());
			System.out.println("=======================");
			
		}*/
		
		//2.获取类的方法
		//方法种类:有参有返回,有参无返回,无参又返回,无参无返回
		//2.1获取类中单个方法(公共的)
		//第一个:方法名
		//第二个:可变参数
		/*Method method= clas.getMethod("setSid", String.class);
		//执行方法,并得到方法的属性
		Object invoke = method.invoke(stu, "图图");
		System.out.println(invoke);*/
		
		//2.2获取类中的单个方法(公共的,私有的,受保护的,最终的等等)
		/*Method method = clas.getDeclaredMethod("add", Integer.class,Integer.class);
		//设置权限
		method.setAccessible(true);
		//执行方法
		Object invoke = method.invoke(stu, 5,5);
		System.out.println(invoke);*/
		
		//2.3获取类中的所有方法
		//Method[] methods = clas.getMethods();
	/*	Method[] methods = clas.getDeclaredMethods();
		for (Method method : methods) {
			System.out.println("方法名:"+method.getName());
			System.out.println("方法的访问修饰符:"+method.getModifiers());
			System.out.println("方法的返回类型:"+method.getReturnType());
			System.out.println("============================");
			
		}*/
		
		//3.构造函数
		//3.1获取类中的单个构造函数(公共的)
		//一个参数
		/*Constructor cont = clas.getConstructor(String.class);
		cont.newInstance("a1");*/
		//两个参数
		/*Constructor cont = clas.getConstructor(String.class,String.class);
		cont.newInstance("a1","图图");*/
		
		/*Constructor con= clas.getDeclaredConstructor(Integer.class);
		con.setAccessible(true);
		con.newInstance(1);*/
		
		//4.获取类对象中所有实现接口列表
		Class[] interfaces = clas.getInterfaces();
		for (Class cla : interfaces) {
			System.out.println("接口名:"+cla.getName());
			
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值