如何在Java中动态地进行方法调用

如何在Java中动态地进行方法调用,java,Java,我正在尝试列出函数调用的列表。我希望能够通过简单地从数组中选择方法来调用特定的方法 例如,如果我想调用drawCircle(),并且该方法位于第一个索引中,那么我可以说runMethod[0] 这是我到目前为止所拥有的。我制作了一个带有两个输入的接口: public interface Instruction { void instr( int a, int b ); } 在我的另一个类中,我有一个方法列表(或者它们应该是实现指令的类?)。我希望能够从列表中调用以下任何方法,如下所

我正在尝试列出函数调用的列表。我希望能够通过简单地从数组中选择方法来调用特定的方法

例如,如果我想调用drawCircle(),并且该方法位于第一个索引中,那么我可以说runMethod[0]

这是我到目前为止所拥有的。我制作了一个带有两个输入的接口:

public interface Instruction {  
   void instr( int a, int b );
} 
在我的另一个类中,我有一个方法列表(或者它们应该是实现指令的类?)。我希望能够从列表中调用以下任何方法,如下所示:

instList[0].mov( 1, 3 );
instList[2].add( 4, 5 );

等等。希望这已经足够清楚了。提前感谢。

除非我误解了您想要实现的目标,否则Java通常的实现方法是实现接口:

interface Instruction {  
    void instr( int a, int b );
}

class MoveInstruction implements Instruction {
    void instr(int a, int b) {
        // do something
    }
}

class AddInstruction implements Instruction {
    void instr(int a, int b) {
        // do something else
    }
}
现在:

Instruction[] instructions = new Instruction[5];
instructions[0] = new MoveInstruction();
instructions[2] = new AddInstruction();

...

instructions[0].instr(1, 3);
instructions[2].instr(4, 5);

如果您在设置数组时只使用了
MoveInstruction
/
AddInstruction
类,那么这是一种加快速度的好方法。

除非我误解了您试图实现的目标,否则Java的常规方法是实现接口:

interface Instruction {  
    void instr( int a, int b );
}

class MoveInstruction implements Instruction {
    void instr(int a, int b) {
        // do something
    }
}

class AddInstruction implements Instruction {
    void instr(int a, int b) {
        // do something else
    }
}
现在:

Instruction[] instructions = new Instruction[5];
instructions[0] = new MoveInstruction();
instructions[2] = new AddInstruction();

...

instructions[0].instr(1, 3);
instructions[2].instr(4, 5);

如果您在设置数组时只使用了
MoveInstruction
/
AddInstruction
类,那么这是一种加快数组速度的好方法。

看起来您尝试的是使用Java中不存在的函数指针,虽然看起来在这种情况下你也不一定需要使用它们。您可以使用这样的常规switch语句

public static void runMethod(int whichone)
{
    switch(whichone)
    {
        case 0: drawCircle(); break;
        case 1: etc...  break;
    }

}
然后您可以使用适当的参数调用runMethod

public static void main(Strign[] args)
{
     runMethod(0);
     runMethod(3);
     etc...
}

看起来您要做的是使用函数指针,这在Java中是不存在的,尽管在这种情况下您似乎也不一定需要使用它们。您可以使用这样的常规switch语句

public static void runMethod(int whichone)
{
    switch(whichone)
    {
        case 0: drawCircle(); break;
        case 1: etc...  break;
    }

}
然后您可以使用适当的参数调用runMethod

public static void main(Strign[] args)
{
     runMethod(0);
     runMethod(3);
     etc...
}

同样,您可以使用映射实现的命令模式

import java.util.HashMap;
import java.util.Map;

public class CommandEg {
   private static Map<String, Instruction> instructionMap = new HashMap<String, Instruction>();

   public static void main(String[] args) {
      instructionMap.put("mov", new Instruction() {
         public void instr(int a, int b) {
            // TODO: code to do mov action
         }
      });
      instructionMap.put("add", new Instruction() {
         public void instr(int a, int b) {
            // TODO: code to do add action
         }
      });

      instructionMap.get("mov").instr(1, 4);
      instructionMap.get("add").instr(4, 8);
   }

}

interface Instruction {  
   void instr( int a, int b );
}
import java.util.HashMap;
导入java.util.Map;
公共类命令{
私有静态映射指令Map=newhashmap();
公共静态void main(字符串[]args){
指令map.put(“mov”,新指令(){
公共无效仪器(内部a、内部b){
//TODO:执行mov操作的代码
}
});
指令map.put(“添加”,新指令(){
公共无效仪器(内部a、内部b){
//TODO:执行添加操作的代码
}
});
说明书Map.get(“mov”)instr(1,4);
说明地图获取(“添加”)说明(4,8);
}
}
接口指令{
无效仪表(内部a、内部b);
}

同样,一种可以通过映射实现的命令模式

import java.util.HashMap;
import java.util.Map;

public class CommandEg {
   private static Map<String, Instruction> instructionMap = new HashMap<String, Instruction>();

   public static void main(String[] args) {
      instructionMap.put("mov", new Instruction() {
         public void instr(int a, int b) {
            // TODO: code to do mov action
         }
      });
      instructionMap.put("add", new Instruction() {
         public void instr(int a, int b) {
            // TODO: code to do add action
         }
      });

      instructionMap.get("mov").instr(1, 4);
      instructionMap.get("add").instr(4, 8);
   }

}

interface Instruction {  
   void instr( int a, int b );
}
import java.util.HashMap;
导入java.util.Map;
公共类命令{
私有静态映射指令Map=newhashmap();
公共静态void main(字符串[]args){
指令map.put(“mov”,新指令(){
公共无效仪器(内部a、内部b){
//TODO:执行mov操作的代码
}
});
指令map.put(“添加”,新指令(){
公共无效仪器(内部a、内部b){
//TODO:执行添加操作的代码
}
});
说明书Map.get(“mov”)instr(1,4);
说明地图获取(“添加”)说明(4,8);
}
}
接口指令{
无效仪表(内部a、内部b);
}

您可以使用简单的开关盒

switch(function)//function is an int value
{
case 1:circle();
case 2:rectangle();
.
.
.
}

您可以使用简单的开关盒来实现

switch(function)//function is an int value
{
case 1:circle();
case 2:rectangle();
.
.
.
}
如果“动态地进行方法调用”,您的意思是对接口进行方法调用,并让Java选择要执行的代码:这只是通过动态分派实现的多态性,因此提供不同的实现:

class Mov implements Instruction {
    void instr(int a, int b) {
        // code for moving here
    }
}

class Add implements Instruction {
    void instr(int a, int b) {
        // code for adding here
    }
}
然后可以使用这些实现的实例,例如在阵列中。在调用
instr
时,动态调度将为每个实例选择正确的代码。但我想知道这样一个通用接口和一系列实现实例是否是一个好的设计。如果可以,我会使用更具体的接口,并将实例保存在单独的变量中。如果您想要实现命令模式,即您

  • 拥有能够处理不同命令集的不同客户端,或者
  • 要对所有命令进行排队、记录或撤消/重做
如果您所说的“动态地进行方法调用”是指,这只是以一种困难的方式实现的,请参阅。

如果您所说的“动态地进行方法调用”是指对接口进行方法调用,并让Java选择执行哪种代码:这只是通过动态调度实现的多态性,因此提供不同的实现:

class Mov implements Instruction {
    void instr(int a, int b) {
        // code for moving here
    }
}

class Add implements Instruction {
    void instr(int a, int b) {
        // code for adding here
    }
}
然后可以使用这些实现的实例,例如在阵列中。在调用
instr
时,动态调度将为每个实例选择正确的代码。但我想知道这样一个通用接口和一系列实现实例是否是一个好的设计。如果可以,我会使用更具体的接口,并将实例保存在单独的变量中。如果您想要实现命令模式,即您

  • 拥有能够处理不同命令集的不同客户端,或者
  • 要对所有命令进行排队、记录或撤消/重做

如果您所说的“动态地进行方法调用”是指,这只是以一种困难的方式实现的,请参阅。

您可能希望了解如何使用。值得称赞的是,您已经使用了一个接口。:)不应该是
instList[0].instr(1,3)?您可能需要查看如何使用。值得称赞的是,您已经使用了一个接口。:)不应该是
instList[0].instr(1,3)