Choosing Java Methods?

I come from the background of Objective-C. In Objective-C, you can store method references with variables @selector, for example @selector(terminate). Then you can simply “apply” those selectors to objects that execute the method described by the selector.

Now I have to program something in Java. Is there something similar in Java? Or a workaround?

EDIT: Basically, I am creating a CLI program. When I read in commands, I have to map commands to specific methods. I was thinking of creating a dictionary that associates a command with a method selector.

+3
source share
2 answers

An anonymous inner class like here

+3
source

. , , - :

public class ReflectionExample {
    private static class A {
        public void foo() {
            System.out.println("fooing A now");
        }
    }

    public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException,
            IllegalAccessException, InvocationTargetException {
        Method method = A.class.getMethod("foo");
        method.invoke(new A());
    }
}

Method.invoke ( ).

EDIT: , , , . , , , String . .

- . , , , - . , . IDE (, Eclipse) , - , .

+4

Source: https://habr.com/ru/post/1788156/


All Articles