请问:如何把一个方法作为另一个方法的参数?举个例子最好 int method1(){ int i=10; return i;}void method2(int x){ ... ...}然后就可以调用method2(method1()); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 其实那应该是第二个方法有个返回值,然后将返回传给第一个方法吧。如:public int method1() { int i=0; .... return i;}public void method2(int i) { ...}public void method3() {.... method2(method1());....}不知是不是这个意思? c是可以用函数指针来实现,java是不行的。不过你可以把方法的名称作为参数传进去,然后根据名称去执行那个方法。如何根据方法名称字符串调用方法,看一下下面的代码。Class Cls = Class.forName("className");Object obj= Cls.newInstance();Method method = (Method)(Cls.getClass().getMethod("methodName");method.invoke(obj);这段代码是假设你要调用的方法是一个没有返回值和参数的方法 你是否要这样:import java.lang.reflect.*;public class TestMethod{ public static void main(String args[]) { TestMethod t = new TestMethod(); t.test(); } public void test() { try { Class[] cls = new Class[0]; System.out.println("TestMethod 1 :"); Method m1 = this.getClass().getDeclaredMethod("test1",cls); testMethod(this,m1); System.out.println("TestMethod 2 :"); Method m2 = this.getClass().getDeclaredMethod("test2",cls); testMethod(this,m2); System.out.println("TestMethod 3 :"); Method m3 = this.getClass().getDeclaredMethod("test3",cls); testMethod(this,m3); } catch(Exception e) { e.printStackTrace(); } } public String test1() { System.out.println("Test 1"); return "t1"; } public int test2() { System.out.println("Test 2"); return 2; } public void test3() { System.out.println("Test 3"); } public void testMethod(Object fromObj,Method method) { try { Object[] objs = new Object[0]; Object obj = method.invoke(fromObj,objs); System.out.println("Return " + obj); } catch(Exception e) { e.printStackTrace(); } }} to yuanqingfei (必定伟大) :应该是说用方法的返回值作为另外一个方法的参数吧? thanks a lot i will do my best to do what i should do.thank you all!!espciallyto the one who get points,... 关于ScheduledThreadPoolExecutor的想法 如何设置jTextField的边距? java如何算出两个日期间双休日是几月几日 “线程安全的对象”,你是如何理解的? 小程序,根据用户输入给出不同的输出 如何使JTable响应它自己已经定义过的按回车键事件 关于生成可执行文件的问题,请进来讨论(200分) 那位对JAVA回调打印非常熟悉的高手解释,为什么实现后的Printable.print回被调用两次呢? 宣传一个好地方,up者就有分。 请帮看看这段菜代码错在哪里? 有哪些行业在用java?用java做系统的哪一块? JTable如何现实出列名呀!!
public int method1() {
int i=0;
....
return i;
}
public void method2(int i) {
...
}
public void method3() {
....
method2(method1());
....
}
不知是不是这个意思?
Class Cls = Class.forName("className");
Object obj= Cls.newInstance();
Method method = (Method)(Cls.getClass().getMethod("methodName");
method.invoke(obj);
这段代码是假设你要调用的方法是一个没有返回值和参数的方法
import java.lang.reflect.*;public class TestMethod
{
public static void main(String args[])
{
TestMethod t = new TestMethod();
t.test();
}
public void test()
{
try
{
Class[] cls = new Class[0];
System.out.println("TestMethod 1 :");
Method m1 = this.getClass().getDeclaredMethod("test1",cls);
testMethod(this,m1);
System.out.println("TestMethod 2 :");
Method m2 = this.getClass().getDeclaredMethod("test2",cls);
testMethod(this,m2);
System.out.println("TestMethod 3 :");
Method m3 = this.getClass().getDeclaredMethod("test3",cls);
testMethod(this,m3);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public String test1()
{
System.out.println("Test 1");
return "t1";
}
public int test2()
{
System.out.println("Test 2");
return 2;
}
public void test3()
{
System.out.println("Test 3");
}
public void testMethod(Object fromObj,Method method)
{
try
{
Object[] objs = new Object[0];
Object obj = method.invoke(fromObj,objs);
System.out.println("Return " + obj);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
i will do my best to do what i should do.
thank you all!!
espcially
to the one who get points
,...