import java.lang.reflect.Method;
public class ReflectTest02 {
public static void main(String[] args) throws Exception{
String className = "ReflectData";
Class clazz = Class.forName(className);
//Method[] methods = clazz.getMethods();
Method[] methods = clazz.getDeclar
edMethods();
for(int i = 0; i < methods.length; i++){
System.out.println(methods[i].getName());
}
}
}
class ReflectData{
static int i;
static{
i = 5;
System.out.println("static block");
}
public void print1(){
System.out.println("print1");
}
public void print2(){
System.out.println("print2");
}
}
import java.lang.reflect.Method;
public class ReflectTest02 {
public static void main(String[] args) throws Exception{
String className = "ReflectData";
String methodName = "print1";
Class clazz = Class.forName(className);
Object obj = clazz.newInstance();
&
nbsp;
// 메소드 정보를 가지고있는 Method 클래스의
// 인스턴스를 반환한다.
Method method = clazz.getMethod(methodName,
null);
// obj가 가지고 있는 메소드를 실행하라. 클래스이름이나 메서드이름이 정해져있지 않을때 실행하는 방법임
method.invoke(obj, null);
ReflectData d = new ReflectData();
d.print1();
}
}
class ReflectData{
static int i;
static{
i = 5;
System.out.println("static block");
}
public void print1(){
System.out.println("print1");
}
public void print1(String str){
System.out.println("print1" + str);
}
public void print2(){
System.out.println("print2");
}
}