如何通过反射使用一个类的方法

本文介绍了Java反射机制的基本用法,包括实例化对象并通过对象调用方法、无须实例化对象直接调用方法、调用带参数的方法以及使用bsh-core-2.0b4.jar包实现反射等四种方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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");
	}


方法3:反射使用带参数的方法:

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");
	}


如果有多个参数只需要在getMethod方法里面传入不同的参数类型,在invoke方法里传入参数即可

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));
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值