当前位置:网站首页>Reflection 【Notes】
Reflection 【Notes】
2022-08-10 05:48:00 【hagong9】
反射的引入
public class ReflectionQuestion {
public static void main(String[] args) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
//根据配置文件re.properties 指定信息 创建cat对象
//传统方法 new 对象 调用方法
/*Cat cat = new Cat();
cat.hi();*/
//使用Properties类,读写配置文件
Properties properties = new Properties();
properties.load(new FileInputStream("src\\re.properties"));
//读取类的全路径
String classfullpath = properties.get("classfullpath").toString();
String method = properties.get("method").toString();
System.out.println("classfullpath="+ classfullpath);
System.out.println("method="+ method);
//2.想要创建对象? 行不通
//3.使用,Reflection mechanism to solve
//(1)加载类 返回了一个Class的对象
Class aClass = Class.forName("reflaction.question.Cat");
//(2)通过cls得到你加载的类 reflection.question.Cat对象实例
Object o = aClass..getDeclaredConstructor().newInstance();
//通过aClass 得到你加载的类reflection.question.Cat的 method的方法对象
//即 在反射中,可以把方法视为对象.
Method method1 = aClass.getMethod(method);
//通过method1 调用方法(The method is invoked through the object of the method)
//传统方法是对象.方法(),The reflection mechanism is the method.invoice(对象)
System.out.println("====================");
method1.invoke(o);
}
}
反射相关的类


反射的优缺点


It can be seen that the use of reflection is a lot slower
public static void m1(){
long star = System.currentTimeMillis();
for (int i = 0; i <1000000 ; i++) {
Cat cat = new Cat();
cat.hi();
}
long end = System.currentTimeMillis();
System.out.println(end - star);
}
//反射方式调用hi
public static void m2() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> aClass = Class.forName("reflaction.question.Cat");
Object o = aClass.getDeclaredConstructor().newInstance();
Method hi = aClass.getMethod("hi");
long star = System.currentTimeMillis();
for (int i = 0; i <1000000 ; i++) {
hi.invoke(o);
}
long end = System.currentTimeMillis();
System.out.println( end - star);
}反射优化

Turn off reflection checking.提供效率


Class类


常用方法
public class Class_ {
private static Class<?> cls;
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
String classPath = "reflaction.question.Cat";
cls = Class.forName(classPath);
//1.获取到Car类 对应的Class对象
System.out.println(cls);
//输出真正的运行类型
System.out.println(cls.getClass());
//2.得到包名
System.out.println(cls.getPackage().getName());
System.out.println(cls.getPackageName());
//3.Get the name of the class name
System.out.println(cls.getName());
//4.通过cls 创建对象实例
Cat cat = (Cat) cls.getDeclaredConstructor().newInstance();
System.out.println(cat);
//5.通过cls Reflection gets object properties 比如name
Field name = cls.getField("name");
System.out.println(name.get(cat));
//通过反射给对象属性赋值
name.set(cat,"咪咪");
System.out.println(name.get(cat));
//Get fields for all attributes
Field[] fields = cls.getFields();
for (Field o :fields) {
System.out.println(o.getName());
}
}
}得到Class类 对象的6种方式
public static void main(String[] args) throws ClassNotFoundException {
//1.Class.forName
String path = "reflaction.question.Cat";
Class<?> cls1 = Class.forName(path);
System.out.println(cls1.getClass());
//2.类名.class 应用场景:参数传递
Class<Cat> cls2 = Cat.class;
System.out.println(cls2.getClass());
//3.对象.getClass() 应用场景,Object instance already exists
Cat cat = new Cat();
Class cls3 = cat.getClass();
System.out.println(cls3.getClass());
//4.通过类加载器来获取Class对象 和3差不多
//(1)先得到类加载器 cat
ClassLoader classLoader = cat.getClass().getClassLoader();
//(2)通过类加载器得到Class对象
Class<?> cls4 = classLoader.loadClass(path);
System.out.println(cls4.getClass());
//5.基本数据类型 obtained as suchClass类
Class<Integer> integerClass = int.class;
Class<Double> doubleClass = double.class;
System.out.println(integerClass);
System.out.println(doubleClass);
//6基本数据类型对应的包装类可以通过.TYPE 得到Class对象
Class<Integer> type1 = Integer.TYPE;
Class<Character> type2 = Character.TYPE;
}哪些类有Class对象.

public static void main(String[] args) {
Class<String> cls1 = String.class;//外部类
Class<Serializable> cls2 = Serializable.class;//接口
Class<Integer[]> cls3 = Integer[].class;//数组
Class<float[]> cls4 = float[].class;//二维|数组
Class<Thread.State> cls5 = Thread.State.class;//枚举
Class<Long> cls6 = long.class;//基本数据类型
Class<Void> cls7 = void.class;//Void数据类型
Class<Class> classClass = Class.class;//ClassIt is also an external class
System.out.println(cls1);
System.out.println(cls2);
System.out.println(cls3);
System.out.println(cls4);
System.out.println(cls5);
System.out.println(cls6);
System.out.println(cls7);
}
动态和静态加载


类加载流程



在准备阶段,Actions performed by properties of a class


通过反射获取类的信息
Multiple modifiers will stack 


通过反射创建对象
Blast is used to access private constructors,方法,属性.
public class Demo01 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
Class<?> aClass = Class.forName("reflaction.createConstructors.Cat");
//1.通过public的无参构造器创建实例
Object o = aClass.getDeclaredConstructor().newInstance();
Field name = aClass.getField("name");
name.set(o,"mimi");
System.out.println(o);
//2.通过public的有参构造器创建实例
//(1).先得到对应的构造器
Constructor<?> declaredConstructor = aClass.getConstructor(String.class );
//(2)创建实例 Then pass in the actual parameter
Object mimi = declaredConstructor.newInstance("咪咪");
System.out.println(mimi);
//3 通过非public的有参构造器创建实例
//(1)先得到private的构造器对象
Constructor<?> declaredConstructor1 = aClass.getDeclaredConstructor(String.class, int.class);
//爆破 Private constructors can be used using reflection
declaredConstructor1.setAccessible(true);
Object mimi1 = declaredConstructor1.newInstance("mimi", 1);
System.out.println(mimi1);
}
} 通过反射 访问类中的成员
public class Demo02 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
Class<?> aClass = Class.forName("reflaction.createConstructors.Cat");
//Manipulate public properties
Object o = aClass.getDeclaredConstructor().newInstance();
Field name = aClass.getField("name");
name.set(o,"小猫");
System.out.println(name.get(o));
//Manipulate private properties
Field color = aClass.getDeclaredField("color");
color.setAccessible(true);
color.set(o,"红");
System.out.println(color.get(o));
}
}通过反射调用方法

public class Demo02 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
Class<?> aClass = Class.forName("reflaction.createConstructors.Cat");
//Manipulate public properties
Object o = aClass.getDeclaredConstructor().newInstance();
Field name = aClass.getField("name");
name.set(o,"小猫");
System.out.println(name.get(o));
//Manipulate private properties
Field color = aClass.getDeclaredField("color");
color.setAccessible(true);
color.set(o,"红");
System.out.println(color.get(o));
//调用公有方法
Method method = aClass.getMethod("eat");
method.invoke(o);
//调用私有方法
Method declaredMethod = aClass.getDeclaredMethod("run");
declaredMethod.setAccessible(true);//爆破
declaredMethod.invoke(o);
}
}边栏推荐
猜你喜欢

深度学习模型训练前的必做工作:总览模型信息

使用Tenserboard可视化深度学习训练过程

pytorch框架学习(9)torchvision.transform

最新最全的数字藏品发售日历-07.26

自适应空间特征融合( adaptively spatial feature fusion)一种基于数据驱动的金字塔特征融合策略

几种绘制时间线图的方法

文章复现:超分辨率网络FSRCNN

Chain Reading|The latest and most complete digital collection sales calendar-07.29

Tkinter 入门之旅

transaction, storage engine
随机推荐
各个架构指令集对应的机型
几种绘制时间线图的方法
Database Notes Create Database, Table Backup
数据库 笔记 创建数据库、表 备份
ZigBee 网络设备相关内容
YOLOv5 PyQt5(一起制作YOLOv5的GUI界面)
Index Notes【】【】
连接 Nacos 报超时错误
智能合约和去中心化应用DAPP
GtkD开发之路
pytorch框架学习(3)torch.nn.functional模块和nn.Module模块
Linux database Oracle client installation, used for shell scripts to connect to the database with sqlplus
tinymce富文本编辑器
你不知道的常规流
网络安全5
【格式转换】将JPEG图片批量处理为jpg格式
第二次实验
pytorch框架学习(9)torchvision.transform
链读 | 最新最全的数字藏品发售日历-07.28
多表查询 笔记
