Created
March 4, 2018 03:04
-
-
Save VintageAppMaker/e2a89cbca874f30363314daa0c134c82 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 제목:reflection을 이용한 동적함수처리 | |
* 작성자: 박성완(adsloader@naver.com) | |
* 목적 : 예제 | |
* */ | |
import java.util.HashMap; | |
import java.lang.reflect.Method; | |
class Event{ | |
public int STATE = 0; | |
public Event (int i ){STATE = i;} | |
} | |
public class Main { | |
public static int START = 0; | |
public static int STOP = 1; | |
public static int PAUSE = 2; | |
//---------------------------------------------------- | |
public void OnStart(Event e) | |
{ | |
System.out.println("Start"); | |
} | |
public void OnStop(Event e) | |
{ | |
System.out.println("Stop"); | |
} | |
public void OnPause(Event e) | |
{ | |
System.out.println("Pause"); | |
} | |
//---------------------------------------------------- | |
HashMap m = new HashMap(); | |
public void Process(Event e) throws Exception | |
{ | |
Class<?> c = Class.forName("Main"); | |
String sFunction = (String)m.get(e.STATE); | |
Method m = c.getDeclaredMethod(sFunction, Event.class); | |
m.invoke(this, e); | |
} | |
public Main() | |
{ | |
initProcess(); | |
} | |
public void initProcess() | |
{ | |
m.put(START, "OnStart"); | |
m.put(STOP, "OnStop"); | |
m.put(PAUSE, "OnPause"); | |
} | |
//----------------------------------------------------- | |
static public void main(String[] args) | |
{ | |
Main o = new Main(); | |
try { | |
o.Process(new Event(Main.START)); | |
o.Process(new Event(Main.STOP)); | |
o.Process(new Event(Main.PAUSE)); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment