如何使构造函数的排序更有效?

时间:2017-03-21 18:47:50

标签: java constructor coding-efficiency

我目前正在开发一个必须拥有所有可能的计算示例的程序。我为数字,差异,产品,模数,商和总和做了一个课。我正在做这样的事情

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 个答案:

答案 0 :(得分:0)

我不确定如何在用户定义的类中使用nCr = (n-1)C(k-1) + (n-1)Ck 运算符,但问题的解决方案是创建一个空接口+,然后将其实现到所有类并具有一个构造函数:

IExample

答案 1 :(得分: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)。像你一样指定更具体的构造函数是可选的(特别是因为所有的构造函数都在做同样的事情)。

答案 2 :(得分: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参数。

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

答案 3 :(得分:0)

哦我做的是创建一个超类Expression然后可以做

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

Difference(Expression e1,  Expression e2) {
            lhs = e1;
            rhs = e2;
}
}