Determining and invoking methods of a class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #16
    Originally posted by nepomuk
    I've been experimenting and here's a working example:
    [CODE=java]
    import java.lang.refle ct.Method;

    public class C3 {

    public static void main(String[] args) {
    C1 c1 = new C1();
    C2 c2 = new C2();
    useClass(c1, "doSomethin g");
    useClass(c2, "doSomethingEls e");
    useClass(c2, "doSomethingThi rd");
    }
    private static void useClass(Object obj, String method)
    {
    try
    {
    Class params[] = {};
    Method meth = obj.getClass(). getDeclaredMeth od(method, params);
    Object paramsObj[] = {};
    meth.invoke(obj , paramsObj);
    }
    catch(NoSuchMet hodException nsme)
    {
    System.out.prin tln("No method " + method + " in " + obj.getClass()) ;
    }
    catch(Exception e)
    {
    e.printStackTra ce();
    }
    }
    }
    class C1 {
    void doSomething()
    {
    System.out.prin tln("Using C1.doSomething( )");
    }
    }
    class C2 {
    void doSomethingElse ()
    {
    System.out.prin tln("Using C2.doSomethingE lse()");
    }
    void doSomethingThir d()
    {
    System.out.prin tln("Using C2.doSomethingT hird()");
    }
    }
    [/CODE]If you need help understanding it, do ask!
    Just hope, it's not considered to much spoon feeding. ^^ If so, a Admin may feel free to delete it.

    Greetings,
    Nepomuk
    What would really help the OP is to read a reflection tutorial. But they are ignoring me anyway ...

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #17
      Originally posted by r035198x
      What would really help the OP is to read a reflection tutorial. But they are ignoring me anyway ...
      This one? Cause even if the OP doesn't read it, I'm interested...

      Greetings,
      Nepomuk

      Comment

      Working...