是否可以将算术运算符传递给java中的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2902458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:19:55  来源:igfitidea点击:

Is it possible to pass arithmetic operators to a method in java?

javaoperatorsmath

提问by James T

Right now I'm going to have to write a method that looks like this:

现在我将不得不编写一个如下所示的方法:

public String Calculate(String operator, double operand1, double operand2)
{

        if (operator.equals("+"))
        {
            return String.valueOf(operand1 + operand2);
        }
        else if (operator.equals("-"))
        {
            return String.valueOf(operand1 - operand2);
        }
        else if (operator.equals("*"))
        {
            return String.valueOf(operand1 * operand2);
        }
        else
        {
            return "error...";
        }
}

It would be nice if I could write the code more like this:

如果我能像这样编写代码就好了:

public String Calculate(String Operator, Double Operand1, Double Operand2)
{
       return String.valueOf(Operand1 Operator Operand2);
}

So Operator would replace the Arithmetic Operators (+, -, *, /...)

所以运算符将替换算术运算符(+、-、*、/...)

Does anyone know if something like this is possible in java?

有谁知道在java中是否有可能发生这样的事情?

采纳答案by Jon Skeet

No, you can't do that in Java. The compiler needs to know what your operator is doing. What you coulddo instead is an enum:

不,你不能在 Java 中做到这一点。编译器需要知道您的操作员在做什么。你可以做的是一个枚举:

public enum Operator
{
    ADDITION("+") {
        @Override public double apply(double x1, double x2) {
            return x1 + x2;
        }
    },
    SUBTRACTION("-") {
        @Override public double apply(double x1, double x2) {
            return x1 - x2;
        }
    };
    // You'd include other operators too...

    private final String text;

    private Operator(String text) {
        this.text = text;
    }

    // Yes, enums *can* have abstract methods. This code compiles...
    public abstract double apply(double x1, double x2);

    @Override public String toString() {
        return text;
    }
}

You can then write a method like this:

然后你可以写一个这样的方法:

public String calculate(Operator op, double x1, double x2)
{
    return String.valueOf(op.apply(x1, x2));
}

And call it like this:

并这样称呼它:

String foo = calculate(Operator.ADDITION, 3.5, 2);
// Or just
String bar = String.valueOf(Operator.ADDITION.apply(3.5, 2));

回答by Snake

No this is not possible in this way.

不,这是不可能的。

You will need a parser to do what you want, and this can be cumbersome.

你需要一个解析器来做你想做的事情,这可能很麻烦。

You're probably asking the wrong question, since you are getting the wrong answer.

你可能问错了问题,因为你得到了错误的答案。

If you are looking for a mathematical parser you might want to take a look at this project on SF: http://sourceforge.net/projects/jep/

如果您正在寻找数学解析器,您可能想看看 SF 上的这个项目:http: //sourceforge.net/projects/jep/

There might be some answers in this.

这里面可能有一些答案。

回答by polygenelubricants

Method arguments in Java must be expressions. An operator by itself is not an expression. This is not possible in Java.

Java 中的方法参数必须是表达式。运算符本身不是表达式。这在 Java 中是不可能的。

You can, of course, pass objects (maybe enumconstants) that represents those operators, and act accordingly, but you can't pass the operators themselves as parameters.

当然,您可以传递enum代表这些运算符的对象(可能是常量),并相应地采取行动,但您不能将运算符本身作为参数传递。



Additional tips

其他提示

Since you're just starting Java, it's best to ingrain these informations early on to ease your future development.

由于您刚刚开始使用 Java,因此最好尽早将这些信息融入其中,以简化您未来的开发工作。

  • Method names starts with lowercase: calculateinstead of Calculate
  • Variable names starts with lowercase: operatorinstead of Operator
  • Doubleis a reference type, the box for primitive type double.
    • Effective Java 2nd Edition, Item 49: Prefer primitive types to boxed primitives
  • Don't return "error...". Instead, throw new IllegalArgumentException("Invalid operator");
  • 方法名称以小写开头:calculate而不是Calculate
  • 变量名以小写开头:operator而不是Operator
  • Double是一个引用类型,原始类型的框double
    • Effective Java 2nd Edition,Item 49:Preferprimitive types to boxed primes
  • 不要return "error..."。反而,throw new IllegalArgumentException("Invalid operator");

See also

也可以看看

回答by Ashish Jindal

Operators, AFAIK, cannot be passed as a parameter in any language (at least that I've come across).

运算符 AFAIK 不能作为任何语言的参数传递(至少我遇到过)。

Reason is that only values (either by copy or by references) can be passed as "values to parameters".

原因是只有值(通过复制或通过引用)可以作为“参数值”传递。

And operators represent no values.

运算符不代表任何值。

回答by Midhat

You can either

你可以

  1. Use a functional language for JVM to implement this part of your code (clojure, scala et el), wrap lambda functions around math operators and pass those functions as parameters

  2. Get an expression evaluator for Java like http://www.singularsys.com/jep/(and there must be many free alternatives as well)

  1. 使用 JVM 的函数式语言来实现这部分代码(clojure、scala 等),将 lambda 函数包装在数学运算符周围并将这些函数作为参数传递

  2. 获得一个像http://www.singularsys.com/jep/这样的 Java 表达式评估器(而且肯定有很多免费的替代品)

回答by Matthew Flaschen

You can't pass operators directly. You could use functors.

您不能直接传递运算符。你可以使用函子

public double Calculate(BinaryFunction<Double, Double, Double> op, double Operand1, double Operand2)
{
  return (double)op.evaluate(Operand1, Operand2);
}

回答by Thomas Kappler

There's only the cumbersome way of doing it with a callback interface. Something like

使用回调接口只有一种繁琐的方法。就像是

interface Operator {
    public Double do(Double x, Double y);
}

Then you implement the operators you need:

然后你实现你需要的操作符:

Operator plus = new Operator() {
    public Double do(Double x, Double y) {
        return x + y;
    }
};

And your generic method takes an Operator and the two arguments:

你的泛型方法需要一个 Operator 和两个参数:

public String Calculate(Operator operator, Double x, Double y) {
    return String.valueOf( operator.do(x, y) );
}

You could also use an enum instead of an interface if you only need a smaller, fixed number of operators.

如果您只需要较小的固定数量的运算符,您也可以使用枚举而不是接口。

回答by Strelok

It would be nice, wouldn't it? But, you just can't do that. You can probably accomplish, something similar by writing your own "operators".

会很好,不是吗?但是,你不能那样做。您可能可以通过编写自己的“运算符”来完成类似的事情。

public interface Operator {
  Double calculate(Double op1, Double op2);
}

public Addition implements Operator {
  @Override
  Double calculate(Double op1, Double op2) { return op1 + op2; }
}

public class Calculator {
  private static Operator ADDITION = new Addition();
  private static Map<String,Operator> OPERATORS = new HashMap<String,Operator>();
  static {
    OPERATORS.put("+",ADDITION);
  }

  public String Calculate(String operator, Double operand1, Double operand2) {
    return String.valueOf(OPERATORS.get(operator).calculate(operand1,operand2);
  }
}

You get the picture how to extend this to many other operators ... and not only Doubles obviously. The advantage of my method is that you can actualy keep your method signature of accepting a Stringoperator.

您了解如何将其扩展到许多其他运营商......显然不仅仅是双打。我的方法的优点是您实际上可以保留接受String运算符的方法签名。