【问题标题】:Assign proper function to the object of a class为类的对象分配适当的功能
【发布时间】:2012-01-17 09:50:13
【问题描述】:

我有一个名为 MyFunctions 的类,它定义了不同的函数 func1、func2 等。我还有一个类 Process 存储分配给此类对象的函数名称:

Process p1 = new Process();
String fName1 = "func1";
p1.setFunctionName(fName1); 
Process p2 = new Process();
String fName2 = "func2";
p2.setFunctionName(fName2); 

为了运行正确的功能,我执行以下操作:

MyFunctions f = new MyFunctions();
if (p.getFunctionName() == "func1") {
   output = f.func1(inputdata);
} else if (p.getFunctionName() == "func2") {
   output = f.func2(inputdata);
}

我不确定这种方法是否有效。有没有其他方法可以解决这个任务?

另一个问题:在JAVA中可以做这样的事情吗?:

String fName = p.getFunctionName();
output = f."+fName+"(input);

【问题讨论】:

标签: java function


【解决方案1】:

首先,这种方法并不完全正确。永远不要使用== 来比较对象。请改用equals():

MyFunctions f = new MyFunctions();
if ("func1".equals(p.getFunctionName())) {
   output = f.func1(inputdata);
} else if ("func2".equals(p.getFunctionName())) {
   output = f.func2(inputdata);
}

其次,我建议你改用enum。

public enum FunctionInvoker {
    func1 {
        public Object invoke(MyFunctions o, Object ... arg) {
             o.func1(arg[0]);
        }
    },
    func2 {
        public Object invoke(MyFunctions o, Object ... arg) {
             o.func2(arg[0], arg[1]);
        }
    },
}

现在用法如下:

String functionName = ...; // probably read from file, etc.
Object result = FunctionInvoker.valueOf(functionName).invoke(args);

【讨论】:

  • 对象结果 = FunctionInvoker.valueOf(functionName).invoke(args);我将其更改为 ...invoke(new MyFunctions(),args);但我仍然无法编译代码。它说方法“调用”是未定义的。
  • 它在包括以下内容后开始工作:public abstract Object invoke(MyFunctions o, Object args);
【解决方案2】:

Java 的Reflection API 很可能对您有用。它允许您(除其他外)在运行时查找类中的方法(例如,当您在字符串中有方法名称时)并以这种方式调用方法。

一个简单的例子:

String inputdata = "Hello";

// Finds the method "func1" in class MyFunctions that takes a String argument
Method method = MyFunctions.class.getMethod("func1", String.class);

// Calls the method on a new MyFunctions object, passing in inputdata
// as the argument
Object result = method.invoke(new MyFunctions(), inputdata);

System.out.println("Return value: " + result);

【讨论】:

  • 它说:java.lang.NoSuchMethodException: cpn_logic.MyFunctions.func1(java.lang.String) at java.lang.Class.getMethod(Unknown Source)
  • @KlausosKlausos 你的班级真的有一个方法func1 接受String 参数吗?这只是一个如何使用反射 API 的示例,而不是从字面上理解。您必须根据自己的程序对其进行调整。
【解决方案3】:

如果您有一组确定的函数,则使用public static 常量来声明函数的名称。

例子:

public static String FUNCTION1 = "func1";
public static String FUNCTION2 = "func2";

对于第二个问题,是的,可以使用 java reflection。

【讨论】:

    【解决方案4】:

    您可以使用reflection 找到要执行的函数。

    例如:

    f.getClass().getDeclaredMethod(p.getFunctionName).invoke();
    

    【讨论】:

      【解决方案5】:

      我不确定这种方法是否有效。 MyFunctions f = new MyFunctions(); if (p.getFunctionName() == "func1") {

      没有。 AFAIK 它不会工作。您正在比较 String 的引用而不是比较它的内容。

      if (p.getFunctionName() == "func1") 
      

      使用.equals 而不是== 来比较两个字符串。


      在 JAVA 中可以做这样的事情吗? output = f."+fName+"(input);

      您可以使用reflection in java 做您想做的事。

      【讨论】:

        【解决方案6】:

        使用枚举解耦:

        public enum Function {
        ONE {
        public void call(Functions functions) {
        functions.func1();
        },
        TWO {
        ...
        };
        

        public abstract void call(Functions 函数); }

        一个过程:

        Process p1 = new Process(Function.ONE);
        MyFunctions f = new MyFunctions();
        p1.call(f);
        

        希望你明白了 :)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-04-20
          • 2018-02-02
          • 2023-03-16
          • 1970-01-01
          • 2021-04-04
          • 1970-01-01
          相关资源
          最近更新 更多