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)}); } }
注意这句Class cls=Class.forName("test.reflect.A"); //import class 在调用class A的时候,根据你自己的具体情况,进行填写。 (我把它放到包里了,但是代码里没有贴出package部分)
//方法能不能得到自己的名字?不明白你是什么意思,还不知道什么情况下要这样做,如果你事先不知道方法名的话, 你怎么调用?如果你是像这样调用 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() 来得到。
你可以用一个类实现想要传递的方法,然后把该类的实例传递进去 有点像callback
我想楼主的意思是这样的:void dynamicMethod(String methodName) { A a = new A(); // 这个地方楼主想实现 a.methodName()方法 }可不可以实现这样的?
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)});
}
}
在调用class A的时候,根据你自己的具体情况,进行填写。
(我把它放到包里了,但是代码里没有贴出package部分)
String name=xxx;
System.out.println(name);
}希望输出 haha可以做到吗?为情飞,帮帮忙,谢了。
你怎么调用?如果你是像这样调用
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()
来得到。
有点像callback
{
A a = new A();
// 这个地方楼主想实现 a.methodName()方法
}可不可以实现这样的?