According name calling getName methods

  According name calling getName methods that is the question, I can see that there is actually a method is based on the name of name calling getName () This method. 
  And the realization of the methods used to achieve the judgement of the string of code as follows: 
  Public Object getOrderby () ( 
  If (ORDER_TITLE.equals (this.ordered)) ( 
  Return getTitle (); 
  ) Else if (ORDER_RENDERDATE.equals (this.ordered)) ( 
  Return getRenderDate (); 
  ) Else if (ORDER_CREATEDATE.equals (this.ordered)) ( 
  Return getCreateDate (); 
  ) Else if (ORDER_LASTMODIFIED.equals (this.ordered)) ( 
  Return getLastModified (); 
  Else () 
  Return getTitle (); 
  ) 
  ) 
  If so get after the new method means to increase this getOrderby () method judgement. 
  Therefore, I decided to transform reflection of this method, first of all, to define a method doGetMethod 
  Private Object doGetMethod (String methodName) throws Exception ( 
  Object result = null; 
  Object [] eo = new Object [] (); 
  String getMethodName = "get" + methodName.toLowerCase (); 
  Method methods = [] getClass (). GetMethods (); 
  For (int i = 0; i <methods.length; i + +) ( 
  Method method = methods [i]; 
  If (getMethodName.equals (method.getName (). ToLowerCase ())) ( 
  Result = method.invoke (this, eo); 
  ) 
  ) 
  Return result; 
  ) Here under methodName getMethodName combination, and then Method [] Method for this, then invoke calling this method invoke here to explain the parameters of this method is the first parameter is specified class includes methods, and the second parameter is specified methods need parameters. 
  Last modified the original getOrderBy 
  Public Object getOrderby () ( 
  Object obj = getTitle (); 
  Try ( 
  Obj = doGetMethod (getOrdered ()); 
  ) Catch (Exception e) ( 
  Return obj; 
  ) 
  Return obj; 
  ) Posted on 2007-04-12 15:36 rocket read (818) Comments (3) edit collections cited 
  Comments: # re: name calling getName methods 2007-04-12 17:36 | G_G 
 
Sofa 
 
Reply comments more 
 
# Re: name calling getName methods 2007-04-12 17:38 | popoer 
 
Good! Back more comments 
 
# Re: name calling getName methods 2007-04-12 19:57 | Liu Ganquan to make simple things complicated, and if it means using fixed if else is better, if necessary to change at any time, using different interface to achieve + reflection both more useful and more Reply Comments 

Recommend Articles

Comments

Leave a Reply