反射读取类并执行类的process方法。 request.getParameter("className") ->com.test.RevokeTask先通过className得到需要执行的类再去执行这个类中的process方法,如何写这个程序呢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Class c = Class.forName("..."); // 获得类定义Object o = c.newInstance(); // 由类定义生成一个实例Object result = c.getMethod("process").invoke(o); // 调用实例的指定方法 随便搞个例子,很简单的import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class ReflectionDemo extends Base{ private int a = 100; private String str = "test"; public int pi = 222; public static final double PI = 3.1415; public void setInt(int i) { this.a = i; } private class InnerClass { private int i = 0; } public void print(String ss) { System.out.println(ss); } /** * @param args * void */ public static void main(String[] args) { ReflectionDemo demo = new ReflectionDemo(); Base bd = new Base(); Base br = new ReflectionDemo(); System.out.println(demo.getClass().getName()); System.out.println(demo.getClass()); try { // 通过Class.forName()可以检查该类是否存在,并返回该类的Class对象 Class bdc = Class.forName("classdemo.Base"); Class brc = Class.forName("classdemo.ReflectionDemo"); Class rc = Class.forName("classdemo.ReflectionDemo"); demo = (ReflectionDemo) Class.forName("classdemo.ReflectionDemo") .newInstance(); // Annotation[] as = rc..getAnnotations(); System.out.println(brc.getDeclaredClasses()[0].getName()); Method m = demo.getClass().getDeclaredMethod("print", new Class[] { "test method".getClass() }); m.invoke(demo, new Object[] { "test method" }); } catch (ClassNotFoundException e) { e.toString(); e.printStackTrace(); } catch (InstantiationException e) { e.toString(); e.printStackTrace(); } catch (IllegalAccessException e) { e.toString(); e.printStackTrace(); } catch (SecurityException e) { // 捕获异常:异常处理 e.printStackTrace(); } catch (NoSuchMethodException e) { // 捕获异常:异常处理 e.printStackTrace(); } catch (IllegalArgumentException e) { // 捕获异常:异常处理 e.printStackTrace(); } catch (InvocationTargetException e) { // 捕获异常:异常处理 e.printStackTrace(); } System.out.println(demo.pi); }}/** * * <基类> <功能详细描述> */class Base{ private int a1 = 100; private String str1 = "test"; public int pi1 = 222; public static final double PI1 = 3.1415; public void setInt(int i) { this.a1 = i; }} public class Test { public static void main(String[] args) { // char[] cs={'1','2','3','4'}; // String str=new String(cs,1,2); // System.out.println(cs); try { Class cls = Class.forName("Test"); Method[] methods = cls.getMethods(); for (Method m : methods) { String name = m.getName(); if ("process".equals(name)) { m.invoke(new Test(), null); return; } } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void process() { System.out.println("process"); }} String className = request.getParameter("className");Class c = Class.forName(className);Method m = c.getDeclaredMethod("process",String.Class);//如果这你的process方法是 process(String s), String.class代表参数类型m.invoke(className.getClass(),参数); cls.getMethod("process",new Class[]{Integer.class, String.class}).invoke(o,new Object[]{1,""});public void process(Integer i, String b) { System.out.println("process"); } JPanel如何添加cmd.exe 让socket的程序运行 请教一道java试题 问个简单for? 窗口交互问题 在线等。。。。救命问题 正则表达式中的group是干什么用的? 请高手们帮忙啊 不知道有没有java的开源项目,偶想加入,学习学习! 请教!DAO和ORM有什么关系? 帮助提示一些有java源代码的站点 Java里面注解是什么? 如何把系统时间作为DATE插入SQL
Object o = c.newInstance(); // 由类定义生成一个实例Object result = c.getMethod("process").invoke(o); // 调用实例的指定方法
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;public class ReflectionDemo extends Base
{
private int a = 100; private String str = "test"; public int pi = 222; public static final double PI = 3.1415; public void setInt(int i)
{
this.a = i;
} private class InnerClass
{
private int i = 0;
} public void print(String ss)
{
System.out.println(ss);
} /**
* @param args
* void
*/
public static void main(String[] args)
{
ReflectionDemo demo = new ReflectionDemo();
Base bd = new Base();
Base br = new ReflectionDemo();
System.out.println(demo.getClass().getName());
System.out.println(demo.getClass());
try
{
// 通过Class.forName()可以检查该类是否存在,并返回该类的Class对象
Class bdc = Class.forName("classdemo.Base");
Class brc = Class.forName("classdemo.ReflectionDemo");
Class rc = Class.forName("classdemo.ReflectionDemo");
demo = (ReflectionDemo) Class.forName("classdemo.ReflectionDemo")
.newInstance();
// Annotation[] as = rc..getAnnotations(); System.out.println(brc.getDeclaredClasses()[0].getName());
Method m = demo.getClass().getDeclaredMethod("print", new Class[]
{ "test method".getClass() });
m.invoke(demo, new Object[]
{ "test method" }); }
catch (ClassNotFoundException e)
{
e.toString();
e.printStackTrace();
}
catch (InstantiationException e)
{
e.toString();
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.toString();
e.printStackTrace();
}
catch (SecurityException e)
{
// 捕获异常:异常处理
e.printStackTrace();
}
catch (NoSuchMethodException e)
{
// 捕获异常:异常处理
e.printStackTrace();
}
catch (IllegalArgumentException e)
{
// 捕获异常:异常处理
e.printStackTrace();
}
catch (InvocationTargetException e)
{
// 捕获异常:异常处理
e.printStackTrace();
} System.out.println(demo.pi);
}}/**
*
* <基类> <功能详细描述>
*/
class Base
{
private int a1 = 100; private String str1 = "test"; public int pi1 = 222; public static final double PI1 = 3.1415; public void setInt(int i)
{
this.a1 = i;
}
}
public class Test {
public static void main(String[] args) {
// char[] cs={'1','2','3','4'};
// String str=new String(cs,1,2);
// System.out.println(cs);
try {
Class cls = Class.forName("Test");
Method[] methods = cls.getMethods();
for (Method m : methods) {
String name = m.getName();
if ("process".equals(name)) {
m.invoke(new Test(), null);
return;
}
} } catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public void process() {
System.out.println("process");
}
}
Class c = Class.forName(className);
Method m = c.getDeclaredMethod("process",String.Class);//如果这你的process方法是
process(String s),
String.class代表参数类型
m.invoke(className.getClass(),参数);
System.out.println("process");
}