SSISO Community

시소당

동적 클래스,메소드

import  java.lang.reflect.*;

class  A  {
public  void  f1()  {
System.out.println("A.f1  called");
}
public  void  f2()  {
System.out.println("A.f2  called");
}
}

class  B  {
public  void  f1()  {
System.out.println("B.f1  called");
}
public  void  f2()  {
System.out.println("B.f2  called");
}
}

public  class  MethPtr4  {
static  void  callMethod(Object  obj,  Method  meth)
throws  Exception  {
meth.invoke(obj,  null);
}

static  void  findMethod(String  cname,  String  mname)
throws  Exception  {
Class  cls  =  Class.forName(cname);
Method  meth  =  cls.getMethod(mname,
new  Class[]{});
callMethod(cls.newInstance(),  meth);
}

public  static  void  main(String  args[])
throws  Exception  {
if  (args.length  !=  2)  {
System.err.println("missing  class/method
names");
System.exit(1);
}

findMethod(args[0],  args[1]);
}
}

594 view

4.0 stars