forName()、newInstance()、getMethod()、getClass()、invoke()理解说明
来源:互联网 发布:软件项目任务书 编辑:程序博客网 时间:2024/06/11 09:26
1、lass.forName( )静态方法的目的是为了动态加载类。在加载完成后,一般还要调用Class下的newInstance( )静态方法来实例化对象以便操作。因此,Class.forName( )是动态加载类是没有用的,其最终目的是为了实例化对象。
一般的实现方式:
String carClassName = "com.etc.test5.Car";
//创建对象
Car car = (Car) Class.forName(carClassName).newInstance();
2、.getClass(); //得到该对象的class,后期可能会用到getField()得到该类声明的属性,getMethod()获取方法对象。
常见用法:
Method method = car.getClass().getMethod(setterMethodName, String.class); //其中setterMethodName是setter方法的名称,String类型。
3、invoke()方法:我理解为反射赋值,比如继续上面的代码:method.invoke(car, carPropertyValue);其中Car类中有一个name变量,那个执行invoke方法后,对象car的name值则为carPropertyValue(在之前声明过)。
总结:
forName返回Class,后需要newInstance实例化;
newInstance返回T;
getClass返回Class,后需要getMethod获取方法;
getMethod返回Method;
invoke无返回值,需要用Method的对象来调用。
- forName()、newInstance()、getMethod()、getClass()、invoke()理解说明
- 关于forName()、newInstance()、getMethod()、getClass()等区别的简略说明
- 关于forName()、newInstance()、getMethod()、getClass()等区别的简略说明
- Class.forName(),Class.forName().newInstance() ,New ,类名.class,,class.forName(),,getClass()
- 关于Class.forName(className).newInstance()的理解
- java反射机制 + Method.invoke解释 getMethod + 反射理解
- java getMethod与invoke
- Class.forName newInstance 介绍
- Class.forName(name).newInstance()
- java Class.forName("") .newInstance();
- class.forname().newInstance()
- forName、loadClass、NewInstance、new
- class.forname().newInstance()
- java Class.forName("") .newInstance();
- .getClass()、.class、.forName()
- getMethod()和invoke()方法应用
- getInterfaces(),getMethod(String name, Class... parameterTypes),getClass
- class.forname().newInstance() 和 class.forname()
- jdbc总结
- ArrayList源码分析(1.7.0_80)
- IAR的const,变量指定绝对地址,函数指定存取区域
- hdu1253 胜利大逃亡
- Python3 Scrapy 安装方法
- forName()、newInstance()、getMethod()、getClass()、invoke()理解说明
- packageOfficialDebug和resourceFile does not exist.
- 字符混编---动态规划
- Python3 SciPy解常微分方程 用Matplotlib演示
- 柱状图绘制
- git 使用操作命令
- 预测响应模型的应用
- 初识Spring-boot
- win10 tensorflow-gpu 环境搭建