Post http://www.cjsdn.com/post/view? Bid = 1 & id = 39182 & sty = 1 & TPG = 1 & age = 0 propose Program Problems frequently encountered in the design, that is, how to solve the control problem of program flow under complicated conditions. Here, I use reflection to provide a solution to give a key and call the function corresponding to the given key. In this way, the original problem is actually converted to the problem of using conditions to generate suitable keys. A feasible solution for generating keys is to convert these conditions into corresponding strings and then connect them.
The basic idea of the solution is to use hashmap to form a Hasse table of a function, mainly using the followingCodeDuplicate class:
Import java. util. hashmap;
Import java. Lang. Reflect .*;
Public class methodmap {
Private hashmap map;
Methodmap (){
Map = new hashmap ();
}
Public void addjumppoint (string key, method jumppoint ){
Map. Put (Key, jumppoint );
}
Public void removejumppoint (string key ){
Map. Remove (key );
}
Public void jumpto (string key, object OBJ, object [] ARGs ){
Try {
Method M = (method) map. Get (key );
M. Invoke (OBJ, argS );
} Catch (Java. Lang. illegalaccessexception e ){
E. printstacktrace ();
} Catch (Java. Lang. Reflect. invocationtargetexception e ){
E. printstacktrace ();
}
}
}
Source codeSee Attachment:
Methodmap.rar (1.55 K)