比如  
public class A
{
   int id=0;
   pulbic int getId(){return id;}   public static void main(String[] args)
   {    
        A a=new A();
        
        //调用a的getId方法,但不直接函数调用,用反射间接实现
        //如何搞?
   }
}

解决方案 »

  1.   

    import java.lang.reflect.*;
    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;
    }
    };
      

  2.   

    二楼正解   
    Method method = a.getClass().getMethod(mname, types);
    Object result = method.invoke(a, new Object[0]);