栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

J2EE基础之反射机制

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

J2EE基础之反射机制

目录

1、什么是反射

2、类类

3、反射实例化

4、反射动态方法调用

5、反射读写属性


一、什么是反射?

        反射Java语言中的一种机制,通过这种机制可以动态的实例化对象,读写属性,调用方法,我们可以调用属性的私有方法了。


二、类类

        类类指的是,一个对象的实体类的类,通俗一点理解:类类是所有类的最大级

        获得类类的三种方法:

        ①、Class.forName(完整类名)         ②、类名:.class         ③、对象:.getClass()
package com.zq.ll;

public class Demo1 {
	public static void main(String[] args) throws Exception {
//		类.class
		Class clz1=Student.class;
		
//		Student stu = new Student();
//		本工程没有这个类,也可以编译通过
//		Object newInstance = Class.forName("com.xnx.reflect.Student");
		
//		2.类实例.getClass()
		Student stu = new Student();
		Class clz2 = stu.getClass();
		
//		3.Class.forName("类的全路径名");
		Class clz3 = Class.forName("com.zq.ll.Student");

		
	}
}

 三、反射实例化         三种方法:
                1、getConstructor        (获得构造器对象)                 2、getDeclaredConstructor     (获得构造器对象,私有化时)                 3、newInstance        (反射实例化)

我们一般实例化对象就是用构造方法去实例化例如:

        Student stu1 = new Student();
        Student stu2 = new Student("s001");
        Student stu3 = new Student("s001","张三");

但是反射实例化不一样,接下来我们举例四种实例化方式:

1.无参数 共有的构造方法

2.有参数 共有的构造方法

3.有多个参数 共有的构造方法 

4.私有的构造方法

package com.zq.ll;

import java.lang.reflect.Constructor;

public class Demo2 {
	public static void main(String[] args) throws Exception {
		Student stu1 = new Student();
		
		
		//获取类类
		Class clz1 = stu1.getClass();
//		默认就是调用无参 公有的 构造函数
//		1.无参数 公有的构造方法
		Student stu4 = clz1.newInstance();
//		2.有参数 公有的构造方法
//		拿到构造器对象
		Constructor c1 = clz1.getConstructor(String.class);
		Student stu5 = c1.newInstance("s001");
//		3.有多个参数 共有的构造方法
		Constructor c2 = clz1.getConstructor(String.class,String.class);
		Student stu6 = c2.newInstance("s001","李四");
		
//		4.私有的构造方法
//		getConstructor只能够获取公有的构造方法,要获取私有的构造器getDeclaredConstructor
		Constructor c3 = clz1.getDeclaredConstructor(Integer.class);
//		打开访问权限
		c3.setAccessible(true);
		Student stu7 = c3.newInstance(18);
	}
}

四、反射动态方法调用         两种方法:
               1、getMethod                2、getDeclaredMethod

我们在尝试了反射实例化以后我们再来试试反射动态调用方法:

反射动态调用方法

1.调用无参的 共有方法

2.调用1个有参的 共有方法

3.调用私有方法

package com.zq.ll;

import java.lang.reflect.Method;

public class Demo3 {
	public static void main(String[] args) throws Exception {
		
//		1.先拿到类类
		Class clz=Student.class;
//		name:方法名	parameterTypes:方法对应的参数
		Method m1 = clz.getMethod("hello");
//		3.调用对应的方法 
//		第一个参数:那个类实例	第二个参数:方法调用时的实参
//		m1.invoke方法调用的返回值 就是 方法对象本身的返回值hello方法的返回值
		Object invoke = m1.invoke(clz.newInstance());
		System.out.println(invoke);
		
//		调用1个有参的 共有方法
		Method m2 = clz.getMethod("hello", String.class);
		Object invoke2 = m2.invoke(clz.newInstance(),"张三");
		System.out.println(invoke2);
		
//		调用私有方法
		Method m3 = clz.getDeclaredMethod("add", Integer.class,Integer.class);
		m3.setAccessible(true);
		Object invoke3 = m3.invoke(clz.newInstance(), 5,5);
		System.out.println(invoke3);
	}
}

五、反射读写属性         两个方法:                1、getDeclaredField                2、getDeclaredFields

我们为什么要反射读写属性呢?因为我们平常如果在不知晓实体类属性的情况下无法判断,该实体类的属性值,所以反射能够帮我们在不知道实体类属性的情况下,清晰的读写属性。

package com.zq.ll;

import java.lang.reflect.Field;

public class Demo4 {
	public static void main(String[] args) throws Exception {
		Student stu = new Student("s003","张三");
		stu.age=20;
//		需求:要拿到这个学生所有的属性及属性值
		System.out.println("age:"+stu.age);
		System.out.println("sname:"+stu.getSname());
		System.out.println("sid:"+stu.getSid());
		
		
//		一切反射从类类开始
		Class clz = stu.getClass();
//		当前类的所有属性对象
		Field[] fields = clz.getDeclaredFields();
		System.out.println(fields.length);
//		for循环中的f代表每一个属性对象
		for (Field f : fields) {
//			打开访问权限
			f.setAccessible(true);
//			通过属性对象拿到属性
			System.out.println(f.getName());
//			拿到当前的属性对象的 属性值
			System.out.println(f.get(stu));
		}
	}
}

扩展:如何获得属性的访问修饰符?

方法:

        Field类的getModifers()方法和Modifier类中的两个方法即可解决:

                通过调用Modifier.toString(field.getModiers())这样一个方法

案例1:学生实体类

 学生实体类一共有三个属性,其修饰符有两种:private 、public

我们通过代码来获取这两个三个属性的修饰符:

package com.zq.ll;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class Demo5 {
	public static void main(String[] args) throws Exception {
		Student stu = new Student();
//		一切反射从类类开始
		Class clz = stu.getClass();
//		当前类的所有属性对象
		Field[] fields = clz.getDeclaredFields();
//		for循环中的f代表每一个属性对象
		for (Field f : fields) {
//			拿到当前的属性对象的 修饰符类型
			System.out.println(Modifier.toString(f.getModifiers()));
		}
	}
}

结果:

控制台打印的结果证明我们成功拿到了三个属性的修饰符! 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/977911.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号