2013-01-23 65 views
0

说我有一个类和一个名为testMethod(String test1,String test 2)的方法。 我也有不同的方法,这将调用哪个方法永远是在另一个类见下动态调用方法名称

public class functional { 

    testMethod(String test1, String test2) { 

     reCallMethod(); 

    } 
} 

reCallMethod(){ 
    testMethod(test1, test2); // ------> This has to be dynamic. I've written the method name as "testMEthod" here. But I want it generalized so that I can use this in any method and not just in "testMethod" 
} 

更多的信息,例如:----------------- --------------

public class test1 { 
public void TestCase1(String param1, String param2, String param3) { 
     try { 
      //Bla Bla Bla 
     } 
     catch (Throwable t) { 
       TestCase_Store_Locator_Verify_Page_Name(param1,param2,param3); //Retry running this method 

     } 
    } 
} 

public class test2 { 
    public void TestCase2(String param1, String param2, String param3, String param4, String Param5) { 
     try { 
      //Bla Bla Bla 
     } 
     catch (Throwable t) { 
       TestCase2(param1,param2,param3,param4,param5); //Retry running this method 

     } 
    } 
} 

像TestCase1和TestCase2我有500次测试。相反,上面做的,我将有一个名为retryLogic常用的方法如下面

public void retryLogic(){ 
//Call the test method in the class which this method is placed. 
} 


So my TestCase1 will look like 

    public class test1 { 
public void TestCase1(String param1, String param2, String param3) { 
     try { 
      //Bla Bla Bla 
     } 
     catch (Throwable t) { 
       retryLogic(); //Retry running this method 

     } 
    } 
} 


    public void TestCase2(String param1, String param2, String param3) { 
     try { 
      //Bla Bla Bla 
     } 
     catch (Throwable t) { 
       retryLogic(); //Retry running this method 

     } 
    } 
} 
+0

它看起来像你正在寻找[反射](http://docs.oracle.com/javase/tutorial/reflect/member/methodInvocation.html)。 – Pshemo

+0

它为什么会变成动态的? –

+0

请注意,在java中,按照惯例,类名以大写字母开头,方法名以小写字母开头。大多数人都遵循这些惯例。 –

回答

1
+0

谢谢!但在我的例子中,我有“testMethod”接受两个参数。但是如果我有另一种方法称为“testMethod2”,它接受三个参数,我仍然想使用“reCallMethod”? –

+0

你可以用反射来做到这一点,看看方法的[invoke](http://docs.oracle.com/javase/6/docs/api/java/lang/reflect/Method.html#invoke(java.lang .Object,%20java.lang.Object ...))方法,它接受可变数量的参数。在运行时,当然你需要知道调用哪个方法以及需要多少参数。 – tttthomasssss

+0

你现在可以查看我编辑的信息,看看你能帮助我吗?谢谢! –

0

在java中不存在具有可变抱着一个方法的引用的方式。其他一些语言,例如javascript,允许这样做。

有一个详细的解决方法本如果传递对象以实现公知的方法名的接口“reCallMethod()”。通常情况下,界面看起来是这样的:

public interface CallMe() { 
    public Object execute(Object parm1, Object parm2); 
} 

虽然返回值和“执行”可能会因没有你的需要而变化的参数。

那么你的代码看起来是这样的:

public class functional { 

    final OtherClass otherInstance = new OtherClass(); 

    testMethod(String test1, String test2) { 

     reCallMethod(new CallMe() { 
      public Object execute(Object parm1, Object par2) { 
       return otherInstance.varyingMethod((String)parm1, (String)parm2); // This is the varying method 
      } 
     }, text1, text2); 
    }); 
} 

reCallMethod(CallMe func, Object parm1, Object parm2){ 
    func.execute(parm1, parm2); 
} 
+0

你可否请现在检查我的编辑信息,看看你能帮助我吗?谢谢! –

0

如果你不想使用反射,还有另一种可能性,这就是策略模式。它没有提供与反射完全相同的功能,但是您可以在运行时更改您调用的方法。你可以使用一个在你的functional类中调用正确方法的类。

例如,如果您functional类有如下定义:

public class functional { 
    public void testMethod (String test1, String test2) { 
     reCallMethod(); 
    } 

    public void anotherMethod (String test1, String test2) { 
     reCallMethod(); 
    } 
} 

你可以有这将定义策略接口的接口:

public interface MyStrategy { 
    void callMethod (String param1, String param2); 
} 

然后有2个不同的策略执行;一个用于每个要调用的方法。例如:

public class TestMethodStrategy implements MyStrategy { 
    private functional myFunctional; 

    public TestMethodStrategy (functional myFunctional) { 
     this.myFunctional = myFunctional; 
    } 

    public void callMethod (String test1, String test2) { 
     myFunctional.testMethod (test1, test2); 
    } 
} 

您以后需要做的所有事情都是根据当前上下文使用适当的策略。

+0

你现在可以检查我编辑的信息,看看你能帮助我吗?谢谢! –

0

也许你可能只是这样做这做同样的事情:

public void TestCase2(String param1, String param2, String param3) { 
     boolean success; 
     do { 
      success = true; 
      try { 
       //Bla Bla Bla 
      } 
      catch (Throwable t) { 
       success = false; 
      } 
     } while (!success); 
    } 

你甚至可以添加一个计数器,以防止其运行下去。只是增加了一些东西,并在20次尝试之后做了break或其他任何事情。

关于它的最大好处是,如果您已经有其他代码编写。您只需复制并超过方法顶部的前4行,并复制并越过底部的最后5行,并检查是否没有任何异常在现有代码中被捕获并被吃掉。