1

我目前正在开发一个程序,该程序必须包含所有可能的计算示例。我为数字、差、积、模、商和总和做了一个类。我正在做这样的事情

Difference(Number first_number,Number second_number){
    s1 = first_number + second_number;
}
Difference(Number first_number, Product first_difference){
    s1 = first_number + first_difference
}
Difference(Product first_difference, Number first_number){
    s1 = first_product + first_number;
}
Difference(Product first_difference, Product second_difference){
    s1 = first_difference + second_difference;
}
Difference(Number first_number, Quotient first_difference){
    s1 = first_number + first_difference;
}
Difference(Quotient first_difference, Number first_number){
    s1 = first_difference + first_number;
}
Difference(Quotient first_difference, Quotient second_difference){
    s1 = first_difference + second_difference;
}
Difference(Number first_number, Difference first_difference){
    s1 = first_number + first_difference;
}
Difference(Difference first_difference, Number first_number){
    s1 = first_number + first_difference;
}
Difference(Difference first_difference, Difference second_difference){
    s1 = first_difference + second_difference;
}
Difference(Number first_number, Sum first_Difference){
    s1 = first_number + first_Difference;
}
Difference(Sum first_Difference, Number first_number){
    s1 = first_Difference + first_number;
}
Difference(Sum first_Difference, Sum second_Difference){
    s1 = first_Difference + second_Difference;
}
Difference(Number first_number, Modulus first_modulus ){
    s1 = first_number + first_modulus;
}
Difference(Modulus first_modulus, Number first_number){
    s1 = first_number + first_ modulus;
}
Difference(Modulus first_modulus, Modulus second_modulus){
    s1 = first_modulus + second_modulus;
}
Difference(Sum first_Difference, Product first_difference){
    s1 = first_Difference + first_difference;
}
Difference(Product first_difference, Sum first_Difference){
    s1 = first_difference + first_Difference;
}
Difference(Sum first_Difference, Quotient first_difference){
    s1 = first_Difference + first_difference;
}
Difference(Quotient first_difference, Sum first_Difference){
    s1 = first_difference + first_Difference;
}
Difference(Sum first_Difference, Difference first_difference){
    s1 = first_Difference + first_difference;
}
Difference(Difference first_difference, Sum first_Difference){
    s1 = first_Difference + first_difference;
}
Difference(Sum first_Difference, Modulus first_modulus ){
    s1 = first_Difference + first_modulus;
}
Difference(Modulus first_modulus, Sum first_Difference){
    s1 = first_Difference + first_modulus;
}
Difference(Difference first_difference, Quotient first_difference){
    s1 = first_difference + first_difference;
}
Difference(Quotient first_difference, Difference first_difference){
    s1 = first_difference + first_difference;
}
Difference(Product first_difference, Quotient first_difference){
    s1 = first_difference + first_difference;
}
Difference(Quotient first_difference, Product first_difference){
    s1 = first_difference + first_difference;
}
Difference(Quotient first_difference, Modulus first_modulus){
    s1 = first_difference + first_modulus;
}
Difference(Modulus first_modulus, Quotient first_difference){
    s1 = first_difference + first_modulus;
}
Difference(Difference first_difference, Quotient first_difference){
    s1 = first_difference + first_difference;
}
Difference(Quotient first_difference, Difference first_difference){
    s1 = first_difference + first_difference;
}
Difference(Difference first_difference, Modulus first_modulus){
    s1 = first_difference + first_modulus;
}
Difference(Modulus first_modulus, Difference first_difference){
    s1 = first_modulus + first_difference;
}
Difference(Difference first_difference, Modulus first_modulus){
    s1 = first_difference + first_modulus;
}
Difference(Modulus first_modulus, Difference first_difference){
    s1 = first_difference + first_modulus;
} 

以确保我的主要课程有各种可能的组合方式。有什么方法可以提高效率吗?我厌倦了一遍又一遍地写这个。

4

4 回答 4

0

Oh what I did was create a super class Expression and then could do

public class Sum extends Expression{
private Expression lhs;
private Expression rhs;

Difference(Expression e1,  Expression e2) {
            lhs = e1;
            rhs = e2;
}
}
于 2017-04-02T01:27:04.660 回答
0

我不确定如何+在用户定义的类中使用运算符,但解决问题的方法是创建一个空接口IExample,然后将其实现到所有类并具有一个构造函数:

Difference(IExample i1, IExample i2) {
    ...
}
于 2017-03-21T18:53:15.983 回答
0

这是一种可能的方法,概述:

打包练习;

import java.util.function.BiFunction;

public class Operation<T> {
  final T value;

  public Operation(T val) {
    this.value = val;
  }

  public Operation<T> op(T operand, BiFunction<T, T, T> func) {
    final Operation<T> result = new Operation(func.apply(value, operand));
    return result;
  }

  @Override
  public boolean equals(Object other) {
    if (this == other) {
      return true;
    }
    if (! (other instanceof Operation)) {
      return false;
    }
    Operation<T> otherOp = (Operation<T>) other;
    return (value == null ? otherOp.value == null : value.equals(otherOp.value));
  }

  @Override
  public int hashCode() {
    return value == null ? 0 : value.hashCode();
  }

  @Override
  public String toString() {
    return "Operation{ " + value + " }";
  }

  public static void main(String[] args) {
    Operation<Integer> oper1 = new Operation<>(17);
    Operation<Integer> operMinus = oper1.op(23, (x, y) -> x - y);
    Operation<Integer> operPlus = oper1.op(23, (x, y) -> x + y);

    System.out.println("oper1 = " + oper1);
    System.out.println("operMinus = " + operMinus);
    System.out.println("operPlus = " + operPlus);
  }
}

您可以将此方法的变体与方法一起使用protected op,基于它预定义锁定操作,而不是让客户端提供func参数。

请注意,我们不会将业务逻辑与构造混合在一起。

于 2017-03-21T20:38:23.690 回答
0

假设 Product、Quotient、Difference、Sum 和 Modulus 都扩展 Number,你可以这样做:

Difference(Number firstNumber, Number secondNumber){
    s1 = firstNumber + secondNumber;
} 

当调用重载的方法或构造函数时,将使用最具体的方法/构造函数。这是一个例子:

Sum s = new Sum(1);
Product p = new Product(2);
Difference d = new Difference(s, p);

将调用带有 Number 参数的构造函数,因为没有像Difference(Sum firstSum, Product firstProduct)这样的更具体的构造函数。像您一样指定更具体的构造函数是可选的(特别是因为所有构造函数都在做同样的事情)。

于 2017-03-21T19:09:00.780 回答