如何返回方法名称由字符串给出的方法的值?

时间:2016-05-19 03:38:14

标签: java oop object reflection

我需要返回一个方法的值,我还需要打印方法的名称,包括调用它的对象。例如:

public class FindMethod {    
public void accessor(String m){
            String amount = "getamount()" ;
            String str="";
            if(m.equals("Receive(int)"))
                str+= "LS."+amount;
            System.out.println(str);
        }

         public static void main(String[] args)
         {

             FindMethod fm = new FindMethod();
             fm.accessor("Receive(int)");

         }
}

结果:LS.getamount()

上述程序将方法名称打印为字符串,其中包含LS为对象的对象,getamount()是另一个类LoanApprovalSystem()的方法。

但是我需要打印结果LS.getamount()将返回的整数值。但我已将LS.getamount()作为字符串返回。我不知道如何从字符串中返回LS.getamount()的实际值。

任何人都可以给我一些想法,我怎样才能返回由字符串给出的方法getamount()的值?我的意思是我可以使用字符串LS.getamount()作为参考来调用类getamount()中的方法LoanApprovalSystem()吗?

课程LoanApprovalSystem()如下:

public class LoanApprovalSystem {

    private static int amount;
    private static String risklevel ;
    private static boolean approve;
    private static boolean message;
    private static String result ;

    public LoanApprovalSystem(){

    }

    void initialize(){
        amount=0;
        risklevel=null;
        approve=false;
        message=false;      
    }

    public void Receive(int req){   
    amount = req;
    }

    public void Asses(int req){

           if (req > 1000 && req <= 5000)
           {
               risklevel = "low";
               approve = true;

           }
           else if (req > 5000 && req <= 10000)
           {
               risklevel = "high";

           }
           else 
               risklevel = " ";
    }

    public void Approval(int req){
        if ((req > 10000) || ((req <= 10000) & getrisklevel() =="high"))
        {
            approve = false;
        }

        else if (amount <= 5000 && getrisklevel() == "low")
           {
            approve = true;
           }
    }

    public void Sendmessage(String risklevel){      
        if(risklevel == "low")
        {
            message=true;
            //System.out.println(message);
            //System.out.println("Loan approved");
        }
        else
            message=false;
    }

    public void Reply(boolean message, boolean approve){
        if(message == true || approve == true)
        {
            result = ("Loan Approved");
            //System.out.println("Loan Approved");
        }
        else
        {
            result = ("Loan Rejected");
            //System.out.println("Loan Rejected");
        }
    }

    public int getamount(){
        return (amount);
    }

    public String getrisklevel(){
        return(risklevel);
    }

    public boolean getapprove(){
        return (approve);
    }

    public boolean getmessage(){
        return(message);
    }

    public String getresult(){
        return (result);
    }


    public String toString(){
        String str = "";
        str += "(" +result+ ").";
        return str;

    }


    public static void main(String[] args){
        LoanApprovalSystem LS = new LoanApprovalSystem();
        TestdataGeneration testdata = new TestdataGeneration();
        LS.initialize();
        //for(int data:testdata.Testdata())
        {
        LS.Receive(testdata.thirddata());
        LS.Asses(LS.getamount());
        LS.Approval(LS.getamount());
        LS.Sendmessage(LS.getrisklevel());
        LS.Reply(LS.getmessage(), LS.getapprove());
        System.out.println("Final state: "+LS);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

使用反射:

http://java.sun.com/docs/books/tutorial/reflect/member/methodInvocation.html

Class<?> c = Class.forName("className");
Method  method = c.getDeclaredMethod ("methodName", parameterTypes)
Object o = method.invoke (objectToInvokeOn, paramList)

但通常你不使用反射。只有没有别的办法。 在这里寻找使用和反思的危险https://softwareengineering.stackexchange.com/questions/123956/why-should-i-use-reflection