比如
public class A
{
int id=0;
pulbic int getId(){return id;} public static void main(String[] args)
{
A a=new A();
//调用a的getId方法,但不直接函数调用,用反射间接实现
//如何搞?
}
}
public class A
{
int id=0;
pulbic int getId(){return id;} public static void main(String[] args)
{
A a=new A();
//调用a的getId方法,但不直接函数调用,用反射间接实现
//如何搞?
}
}
public class test { public static void main(String[] args) throws Exception{
A a = new A();
a.setA("lslsl");
String mname = "getA";
//Object obj = new A();
Class[] types = new Class[] {};
Method method = a.getClass().getMethod(mname, types);
Object result = method.invoke(a, new Object[0]);
System.out.println(result); }
}
class A{
private String a;
public void setA(String a){
this.a = a;
}
public String getA(){
return a;
}
};
Method method = a.getClass().getMethod(mname, types);
Object result = method.invoke(a, new Object[0]);