English 中文(简体)
• 如何在 j瓦的传单上建立功能?
原标题:How to build a function on the fly in java?
  • 时间:2010-03-29 00:29:27
  •  标签:
  • java

我把正在编成某些ava法的正文文件编成:

public void eval(Node arg)
    {
        if(arg.data.equals("rand"))
        {
            moveRandomly();
        }
        else if(arg.data.equals("home"))
        {
            goHome();
        }
            else if(arg.data.equals("iffood"))
    {
        ifFoodHere(arg.left, arg.right);
    }//snip..

这将需要重新评价,大约需要1 000次,我不得不每次绕过整个东西。 难道有什么办法可以一劳永逸地完成这项工作,然后它就能够成为一种相互称之为的职能?

问题回答

您可以绘制一份可移动地图:

Map<String, Runnable> methods = new HashMap<String, Runnable>();
methods.put("rand", new Runnable() 
{
    public void run()
    {
        moveRandomly();
    }
});
...

然后,用你的方法

public void eval(Node arg)
{
    Runnable command = methods.get(arg.data);
    command.run();
}

创建匿名内班。

类似:

public Callable<Void> eval(Node arg)
{
  if(arg.data.equals("rand"))
  {
    return new Callable<Void>{ public Void call() { moveRandomly(); return null; } };
  }
  ...
}

Callable<Void> f = eval(a);
f.call();

如果你知道你可以期望的所有论点/建议,那么,我可以这样做:

enum Args {
  home, rand, iffood;

  private Method method;

  private Args () {
    try {
      this.method = Commands.class.getMethod(this.name(), Node.class);
    } catch (final Exception e) {}
  }
  public void invoke (final Node args)
    throws IllegalArgumentException, IllegalAccessException,
      InvocationTargetException {
    this.method.invoke(null, args);
  }
  public static Args valueOf (final Node arg) {
    return valueOf(arg.data);
  }
  public static void eval (final Node arg)
    throws IllegalArgumentException, IllegalAccessException,
      InvocationTargetException {
    valueOf(arg).invoke(arg);
  }
}

执行指挥:

class Commands {    
  public static void home (final Node arg) {
    goHome(); // Call the implementation
    // or simply make these bodies the implementations.
  }
  public static void iffood (final Node arg) {
    ifFoodHere(arg.left, arg.right);
  }    
  public static void rand (final Node arg) {
    moveRandom();
  }
  //...
}

之后,你就变成了:

try {
  Args.eval(arg);
} catch (IllegalArgumentException e) {
  // Handle unknown arg.data
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签