如何把方法名作为参数传递?

MAX°孟兆 2004-04-15 12:16:04
如有一方法:
public void myMethod(int i)
{
...
}

如何把这个方法作为参数传递给别的方法?
...全文
769 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
MAX°孟兆 2004-04-15
  • 打赏
  • 举报
回复
能不能说得具体点?
山卜居士 2004-04-15
  • 打赏
  • 举报
回复
试试java.lang.reflect 里的反射API吧。
pillar110 2004-04-15
  • 打赏
  • 举报
回复
java不能,C可以
eshow 2004-04-15
  • 打赏
  • 举报
回复
为什么要传方法名做参数呢?楼主想要什么用途?
cyicecream 2004-04-15
  • 打赏
  • 举报
回复
用反射机制可以解决这个问题,看看thinking in java里,应该有吧,我8晓得在哪看过了哈
pking2002 2004-04-15
  • 打赏
  • 举报
回复
我想楼主的意思是这样的:

void dynamicMethod(String methodName)
{
A a = new A();
// 这个地方楼主想实现 a.methodName()方法
}

可不可以实现这样的?
tiger_wkh52741 2004-04-15
  • 打赏
  • 举报
回复
间接的可以实现。。。
SmileAndHappy 2004-04-15
  • 打赏
  • 举报
回复
你可以用一个类实现想要传递的方法,然后把该类的实例传递进去
有点像callback
flyforlove 2004-04-15
  • 打赏
  • 举报
回复
//方法能不能得到自己的名字?

不明白你是什么意思,还不知道什么情况下要这样做,如果你事先不知道方法名的话,
你怎么调用?

如果你是像这样调用
Method[] mth=cls.getMethods(); //get all methods
mth[0].invoke(obj,new Object[]{new String("hello")}); //invoke the first method
mth[1].invoke(obj,new Object[]{new Integer(3)}); //invoke the second method
mth[2].invoke(obj,null); //invoke the third method
mth[3].invoke(obj,new Object[]{new Byte((byte)60)});

想要得到函数名的话,可以用
mth[0].getName()
来得到。
盐木鱼 2004-04-15
  • 打赏
  • 举报
回复
java中的反射很容易实现

haha1903 2004-04-15
  • 打赏
  • 举报
回复
方法能不能得到自己的名字?

有这样的方法吗?

public void haha() {
String name=xxx;
System.out.println(name);
}

希望输出 haha

可以做到吗?

为情飞,帮帮忙,谢了。
flyforlove 2004-04-15
  • 打赏
  • 举报
回复
注意这句Class cls=Class.forName("test.reflect.A"); //import class
在调用class A的时候,根据你自己的具体情况,进行填写。
(我把它放到包里了,但是代码里没有贴出package部分)
flyforlove 2004-04-15
  • 打赏
  • 举报
回复
public class A {
public A(String s){
System.out.println(s);
}
public void method_1(String s){
System.out.println("method_1:"+s+" (String type)");
}
public void method_2(int i){
System.out.println("method_2:"+i+" (int type)");
}
public void method_3(){
System.out.println("method_3 (no parameter)");
}
public void method_4(byte b){
System.out.println("mehtod_4:"+b+" (char type)");
}
}

public class B {

public static void main(String[] args) throws Exception{
Class cls=Class.forName("test.reflect.A"); //import class
Constructor cst=cls.getConstructor(new Class[]{String.class}); //get the constructor
Object obj=cst.newInstance(new Object[]{new String("invoke Construct method!")}); //get a new instance

Method m1=cls.getMethod("method_1",new Class[]{String.class}); //get the method_1
Method m2=cls.getMethod("method_2",new Class[]{int.class}); //get the method_2
Method m3=cls.getMethod("method_3",null); //get the method_3
Method m4=cls.getMethod("method_4",new Class[]{byte.class});

m1.invoke(obj,new Object[]{new String("hello")}); //invoke the method_1
m2.invoke(obj,new Object[]{new Integer(123)}); //invoke the method_2
m3.invoke(obj,null); //invoke the method_3
m4.invoke(obj,new Object[]{new Byte((byte)90)});

Method[] mth=cls.getMethods(); //get all methods
mth[0].invoke(obj,new Object[]{new String("hello")}); //invoke the first method
mth[1].invoke(obj,new Object[]{new Integer(3)}); //invoke the second method
mth[2].invoke(obj,null); //invoke the third method
mth[3].invoke(obj,new Object[]{new Byte((byte)60)});
}
}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧