JAVA-使用函数接口进行计算

JAVA-使用函数接口进行计算,java,functional-interface,Java,Functional Interface,所以。。。这是我在Stack上的第一篇文章,我对Java(编程)也是新手。我正在尝试制作一个简单的命令行应用程序,根据产生的收入计算员工的利润。已经做过了,但是现在我正在学习函数接口和lambda,我想尝试使用它们。你可以在下面找到代码 package BalanceCounter; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public s

所以。。。这是我在Stack上的第一篇文章,我对Java(编程)也是新手。我正在尝试制作一个简单的命令行应用程序,根据产生的收入计算员工的利润。已经做过了,但是现在我正在学习函数接口和lambda,我想尝试使用它们。你可以在下面找到代码

package BalanceCounter;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.print("Number of employees: ");
    int empCounter = input.nextInt();
    System.out.println();

    List<Employee> employees = new ArrayList<>(empCounter);
    for (int i = 0; i < empCounter; i++) {
        employees.add(new Employee());
    }

    for (Employee employee : employees) {
        System.out.println("Employee: " + employee.getEmpName()
                + "\nBase salary: " + employee.getEmpBaseSalary()
                + "\nProfit from income: " + employee.getEmpBonus()
                + "\n");
    }
}
}
很抱歉发了这么长的帖子,但我想告诉你我所有的信息。 如果你在我的代码中看到一些错误和坏习惯,请告诉我。亲切问候。

此界面:

public interface Calculation {
    double calculate(double arg);
}
与双一元运算符具有相同的合同:

@FunctionalInterface
public interface DoubleUnaryOperator {
    double applyAsDouble(double operand);
   ...
}
因此,您实际上不需要为此引入新的接口。
您可以使用内置的。
但是如果您想真正强调方法的名称和/或参数。

一般来说,在创建内置功能接口之前,请先查看它是否与您的需求不匹配。

假设现在取消对方法的注释,并希望使用此方法:

  double getEmpBonus(DoubleUnaryOperator calculation) {
     return empBonus = calculation.applyAsDouble(this.empBonus);
  }
如果不使用lambda,您必须创建一个子类或匿名类,如果您想通过一个实现,从而使奖金加倍:

class DoubleBonus implements DoubleUnaryOperator {
    public double applyAsDouble(double operand) {
        return operand * 2;
    }
}
lambda的思想是可以用lambda表达式内联子类。
您可以这样调用该方法:

double doubledBonus = getEmpBonus(empBonus->empBonus*2);

你不再需要子类或匿名类了。

那么,你想让我们用lambda表达式和函数接口来写这个吗?我不确定你想问的具体问题是什么。相反,你似乎只是在概括地描述你对下一步项目的打算。是的,这就是我希望你能帮助我的。如你所见,我自己试过,但效果很差。使用接口和lambda以及简单类型(如int、String等)没有问题。只是不知道如何使用对象@csm_dev-我想自己做这件事,但不知道为什么。对不起,没有说得太具体。也许我应该问一下。。。CalcBonus类应该是怎样工作的呢?现在我非常困惑-你说过你可以使用简单的数据类型,比如String、int等。。。但是,如果你不知道如何使用
empBonus
,那只是一个字符串。请说得更具体些。如果您有错误/异常,请告诉我们。如果代码产生不正确的结果-请告诉我们。你似乎做了一次尝试,但我们不确定这种尝试有什么问题。我不认为lambdas适合这种问题。谢谢你的解释,并推动我前进。整个代码是为了让我理解接口而编写的——只是为了练习。我发现,我也可以通过Main类中的'employee.getEmpBonus(new CalcBonus())'获得正确的输出。虽然您的代码可能会回答这个问题,但提倡您的解决方案是一个很好的实践。
package training;
public class Calculator {
    interface IntegerMath {
        int operation(int a, int b);
    }
    interface RealMath {
        double operation(double a, double b);
    }
    public int operateBinary(int a, int b, IntegerMath op) {
        return op.operation(a, b);
    }
    public double operateBinary(int a, int b, RealMath op) {
        return op.operation(a, b);
    }
    public static void main(String... args) {
        Calculator cal = new Calculator();
        IntegerMath addition = (a, b) -> a + b;
        IntegerMath subtraction = (a, b) -> a - b;
        IntegerMath multiplication = (a, b) -> a * b;
        RealMath division = (a, b) -> a / b;
                System.out.println("Add: 40 + 22 = " +
                cal.operateBinary(40, 22, addition));
        System.out.println("Sub: 120 - 80 = " +
                cal.operateBinary(120, 80, subtraction));
        System.out.println("Mul: 12 * 12 = " +
                cal.operateBinary(12, 12, multiplication));
        System.out.println("Div: 478 / 12 = " +
                cal.operateBinary(478, 12, division));
    }
}
double doubledBonus = getEmpBonus(empBonus->empBonus*2);
package training;
public class Calculator {
    interface IntegerMath {
        int operation(int a, int b);
    }
    interface RealMath {
        double operation(double a, double b);
    }
    public int operateBinary(int a, int b, IntegerMath op) {
        return op.operation(a, b);
    }
    public double operateBinary(int a, int b, RealMath op) {
        return op.operation(a, b);
    }
    public static void main(String... args) {
        Calculator cal = new Calculator();
        IntegerMath addition = (a, b) -> a + b;
        IntegerMath subtraction = (a, b) -> a - b;
        IntegerMath multiplication = (a, b) -> a * b;
        RealMath division = (a, b) -> a / b;
                System.out.println("Add: 40 + 22 = " +
                cal.operateBinary(40, 22, addition));
        System.out.println("Sub: 120 - 80 = " +
                cal.operateBinary(120, 80, subtraction));
        System.out.println("Mul: 12 * 12 = " +
                cal.operateBinary(12, 12, multiplication));
        System.out.println("Div: 478 / 12 = " +
                cal.operateBinary(478, 12, division));
    }
}