Skip to content

Instantly share code, notes, and snippets.

@VintageAppMaker
Created March 4, 2018 03:04
Show Gist options
  • Save VintageAppMaker/e2a89cbca874f30363314daa0c134c82 to your computer and use it in GitHub Desktop.
Save VintageAppMaker/e2a89cbca874f30363314daa0c134c82 to your computer and use it in GitHub Desktop.
/*
* 제목: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