当前位置:网站首页>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);
}
}
边栏推荐
猜你喜欢
文章复现:超分辨率网络FSRCNN
One step ahead, don't miss it again, the chain reading APP will be launched soon!
MySql 约束
用Pytorch从0到1实现逻辑回归
Index Notes【】【】
Chained Picks: Starbucks looks at digital collectibles and better engages customers
最新最全的数字藏品发售日历-07.27
非会员更改有道云笔记背景
链读|最新最全的数字藏品发售日历-08.02
基于Qiskit——《量子计算编程实战》读书笔记(一)
随机推荐
各个架构指令集对应的机型
Tkinter 入门之旅
智能合约和去中心化应用DAPP
25张炫酷交互图表,一文入门Plotly
pytorch框架学习(1)网络的简单构建
毫米波雷达基础概念学习
链读 | 最新最全的数字藏品发售日历-07.28
行盒子的盒模型
链读推荐:从瓷砖到生成式 NFT
基本比例尺标准分幅编号流程
MySql constraints
视图【】【】【】【】
【格式转换】将JPEG图片批量处理为jpg格式
用Pytorch从0到1实现逻辑回归
pytorch框架学习(5)torchvision模块&训练一个简单的自己的CNN (二)
CSDN Markdown 之我见代码块 | CSDN编辑器测评
Batch add watermark to pictures batch add background zoom batch merge tool picUnionV4.0
shell脚本中利用sqlplus操作数据库
R中设置图形参数--函数par()详解
2021-07-09