0

每个 classA实例都有一个 class 实例B。应根据其成员变量A调用不同的方法。这是一个实现我想要的实现:Bmethod_num

public class A {
    private B myB = new B();
    public int method_num = 1;
    public callBMethod() {
        if ( method_num == 1 )
            myB.method1();
        else
            myB.method2();
    }
}

public class B {
    public method1() { }
    public method2() { }
}

myA.method_num = 1我不想做,而是希望能够以某种方式通过 Bmethod1method2直接。我怎样才能做到这一点?

4

5 回答 5

4

如果您不想使用反射(这是一个很好的目标),那么enums 有一些简洁的功能可以让您将 an 设置enum为代理。

public class A {
  private B myB = new B();
  public int method_num = 1;

  public void callBMethod() {
    // Could do it by name.
    BMethods.valueOf("method1").call(myB);
    // Or by number.
    BMethods.values()[method_num].call(myB);
  }

}

enum BMethods{
  method1 {
    @Override
    public void call(B b) {
      b.method1();
    }
  },
  method2 {
    @Override
    public void call(B b) {
      b.method2();
    }
  };

  public abstract void call (B b);
}

public class B {
  public void method1() {
  }

  public void method2() {
  }

}
于 2013-09-10T12:00:20.340 回答
3

I think you can use reflection like this:

java.lang.reflect.Method method;
try {
  method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..);
} catch (SecurityException e) {
  // ...
} catch (NoSuchMethodException e) {
  // ...
}  

try {
  method.invoke(obj, arg1, arg2,...);
} catch (IllegalArgumentException e) {  //do proper handling
} catch (IllegalAccessException e) {//do proper handling
} catch (InvocationTargetException e) {//do proper handling
于 2013-09-10T11:53:39.713 回答
3

你不能。Java 不将函数视为第一类对象,因为它没有 Python 或 C# 之类的函数特性。

您可以创建一个 Command 接口并传递该对象引用:

public interface Command {
    void execute(Object [] args);
}
于 2013-09-10T11:51:46.163 回答
2

也许与Runnable对象?您可以从 B 传递一个 runnable,并.run()直接从 A调用

于 2013-09-10T11:55:16.670 回答
0

Java 反射 API 为您提供了一种方法,可以将 Method 类型的对象与目标对象一起传递,然后可以在目标对象上调用该方法。

示例如下:

Method m; // The method to be invoked

  Object target; // The object to invoke it on

  Object[] args; // The arguments to pass to the method

  // An empty array; used for methods with no arguments at all.
  static final Object[] nullargs = new Object[] {};

  /** This constructor creates a Command object for a no-arg method */
  public Command(Object target, Method m) {
    this(target, m, nullargs);
  }

  /**
   * This constructor creates a Command object for a method that takes the
   * specified array of arguments. Note that the parse() method provides
   * another way to create a Command object
   */
  public Command(Object target, Method m, Object[] args) {
    this.target = target;
    this.m = m;
    this.args = args;
  }

  /**
   * Invoke the Command by calling the method on its target, and passing the
   * arguments. See also actionPerformed() which does not throw the checked
   * exceptions that this method does.
   */
  public void invoke() throws IllegalAccessException,
      InvocationTargetException {
    m.invoke(target, args); // Use reflection to invoke the method
  }
于 2013-09-10T11:56:10.757 回答