如何通过反射使用一个类的方法
来源:互联网 发布:聊天机器人 知乎 编辑:程序博客网 时间:2024/05/22 14:44
Java原生态的反射
说明一下 FanSheTime 为我自己随便用的一个类名
用法1:实例化对象,在通过对象直接使用里面的方法即可
public static void main(String[] args) {try {long begin = System.currentTimeMillis();Class<?> forName = Class.forName("lcl.FanSheTime");FanSheTime ss = (FanSheTime) forName.newInstance();ss.function1("hello world");long end = System.currentTimeMillis();System.out.println("耗时>>>>>>>>>>>>>>>>>>>>"+(end - begin));} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SecurityException e) {e.printStackTrace();} catch (InstantiationException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}catch (NoSuchMethodException e) {e.printStackTrace();}catch (IllegalArgumentException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}}public void function1(String str){System.out.println(str);}public void function1(){System.out.println("hello world");}
方法2:通过反射使用无参数的方法(不实例化对象)
public static void main(String[] args) {try {long begin = System.currentTimeMillis();Class<?> forName = Class.forName("lcl.FanSheTime");Method method = forName.getMethod("function1");method.invoke(forName.newInstance());long end = System.currentTimeMillis();System.out.println("耗时>>>>>>>>>>>>>>>>>>>>"+(end - begin));} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SecurityException e) {e.printStackTrace();} catch (InstantiationException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}catch (NoSuchMethodException e) {e.printStackTrace();}catch (IllegalArgumentException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}}public void function1(String str){System.out.println(str);}public void function1(){System.out.println("hello world");}
public static void main(String[] args) {try {long begin = System.currentTimeMillis();Class<?> forName = Class.forName("lcl.FanSheTime");Method method = forName.getMethod("function1",String.class);method.invoke(forName.newInstance(),"焦剑锋");long end = System.currentTimeMillis();System.out.println("耗时>>>>>>>>>>>>>>>>>>>>"+(end - begin));} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SecurityException e) {e.printStackTrace();} catch (InstantiationException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}catch (NoSuchMethodException e) {e.printStackTrace();}catch (IllegalArgumentException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}}public void function1(String str){System.out.println(str);}public void function1(){System.out.println("hello world");}
Method method = forName.getMethod("function1",String.class,,String.class,int.class);
method.invoke(forName.newInstance(),"焦剑锋","hello world",1);
方法4:通过bsh-core-2.0b4.jar jar包完成反射
这种方法的好处是,你只需要传入响应的对象与方法名即可通过eval方法进行执行想使用的方法,非常容易
package lcl;import bsh.EvalError;import bsh.Interpreter;public class InterpreterWrapper extends Interpreter {public static void main(String[] args) {FanSheTime fanSheTime = new FanSheTime();InterpreterWrapper ip = new InterpreterWrapper();long begin = System.currentTimeMillis();try {ip.set("fanSheTime", fanSheTime);ip.eval("fanSheTime.function1()");} catch (EvalError e1) {e1.printStackTrace();}long end = System.currentTimeMillis();System.out.println("耗时>>>>>>>>>>>>>>>>>>>>"+(end - begin));}}
阅读全文
0 0
- 如何通过反射使用一个类的方法
- 使用反射调用一个类的方法
- java如何通过反射 激活静态类的静态方法
- 如何通过反射调用对象的方法?
- 通过一个例子学习反射的使用
- 如何通过反射来调用类方法
- 如何通过反射调用方法?
- 通过反射构造一个类的实例
- 一个通过反射改变request的请求参数的方法
- 一个通过反射改变request的请求参数的方法
- 一个通过反射改变request的请求参数的方法
- 使用反射获取一个类的所有方法和字段
- 通过Invoke反射类的private方法
- 通过反射调用类内部的方法
- 通过反射调用类里面的方法
- 反射第四步 : 通过反射获取某一个类的 方法 并通过反射调用
- 如何通过反射调用类中的<泛型>方法
- 如何通过Java反射访问类里面的私有方法和变量
- 数据库中的事务是什么
- 第一天
- vim 插件 Ycm安装
- broadcom Knet 功能调试记录
- Minicom安装和使用和lte调试方法
- 如何通过反射使用一个类的方法
- Spring 启动时报No active profile set, falling back to default profiles default
- Maven与Gradle项目互转
- AP_应付税务预扣税Withholding Tax中付款时产生预扣税(案例)
- 《C语言程序设计教程》(主编黄迪明、余勤)第七章课后习题答案
- 1、蓝牙核心技术了解(蓝牙协议、架构、硬件和软件笔记)
- sklearn.preprocessing.Binarizer
- MySQL IFNULL()函数用法MySQL
- 中文域名目前的注册情况怎么样?