除了 SO 用户写的问题之外,您正在做的算术也会有更多问题。由于 Java 不支持运算符重载。这对您来说意味着如果您有两个类对象LiczbaZespolona
(ComplexNumber),编译器将不知道如何处理它并且将无法编译。
为了解决这个问题,你需要两件事在某个类中实现这些数字的算术。当然,您需要一些点才能开始,这个界面可能会有所帮助
private static interface IComplexNumber {
public IComplexNumber add(IComplexNumber complexNumber);
public IComplexNumber substract(IComplexNumber complexNumber);
public IComplexNumber multiply(IComplexNumber complexNumber);
public IComplexNumber divide(IComplexNumber complexNumber);
}
然后,当您知道如何添加两个 ComplexNumber 时,您将需要一些可以为您执行操作的东西。对于这种类型的操作,最好的方法是枚举类型。
这门课可以从这个开始
private enum CompleNumberCalculation {
ADD('+') {
@Override
protected IComplexNumber operation(IComplexNumber left, IComplexNumber right) {
return left.add(right);
}
};
private char operator;
private CompleNumberCalculation(char operator) {
this.operator = operator;
}
protected abstract IComplexNumber operation(IComplexNumber left, IComplexNumber right);
}
为了帮助你,我写了一个简单的MathOperation
枚举来解决RegularNumber的计算
public enum RegularMathOperation {
ADD('+') {
@Override
protected BigDecimal operation(BigDecimal left, BigDecimal right) {
return left.add(right);
}
},
SUBSTRACT('-') {
@Override
protected BigDecimal operation(BigDecimal left, BigDecimal right) {
return left.subtract(right);
}
},
MULTIPLY('*') {
@Override
protected BigDecimal operation(BigDecimal left, BigDecimal right) {
return left.multiply(right);
}
},
DIVIDE('/') {
@Override
protected BigDecimal operation(BigDecimal left, BigDecimal right) {
return left.divide(right,MathContext.DECIMAL64);
}
};
private final char operator;
private RegularMathOperation(char operator) {
this.operator = operator;
}
public char operator() {
return this.operator;
}
public <T1 extends Number, T2 extends Number> T1 calculate(T1 left, T2 right) {
validateInput(left, right);
Class<? extends Number> resultType = left.getClass();
BigDecimal result = this.operation(toBigDecimal(left), toBigDecimal(right));
return (T1) toType(resultType, result);
}
protected abstract BigDecimal operation(BigDecimal left, BigDecimal right);
private void validateInput(Number left, Number right) {
if(left == null) {
throw new IllegalArgumentException("Value of left argument must not be null to perform operation " + this.name());
}
if(right == null) {
throw new IllegalArgumentException("Value of left argument must not be null to perform operation " + this.name());
}
}
private BigDecimal toBigDecimal(Number value) {
if(value instanceof BigDecimal) {
return (BigDecimal) value;
}
return new BigDecimal(value.doubleValue());
}
private Number toType(Class<? extends Number> type, BigDecimal value) {
if(Double.class.isAssignableFrom(type)) {
return type.cast(value.doubleValue());
}
if(Long.class.isAssignableFrom(type)) {
return type.cast(value.longValue());
}
if(Integer.class.isAssignableFrom(type)) {
return type.cast(value.intValue());
}
//...
throw new IllegalStateException("Type not support: "+type);
}
public static Map<Character, RegularMathOperation> operatorMap = new HashMap<Character, RegularMathOperation>();
static {//Fill map
for(RegularMathOperation mathOperation : RegularMathOperation.values()) {
operatorMap.put(mathOperation.operator(), mathOperation);
}
}
public static RegularMathOperation valueOf(char operator) {
RegularMathOperation mathOperation = operatorMap.get(Character.valueOf(operator));
if(mathOperation == null) {
throw new IllegalArgumentException("Could not find MathOperator for operartor: " + operator);
}
return mathOperation;
}
public static <T1 extends Number, T2 extends Number> T1 resultOf(char operator, T1 left, T2 right) {
return RegularMathOperation.valueOf(operator).calculate(left, right);
}
public static void main(String[] args) {
Long left = 3L;
Double right = 2D;
System.out.println(RegularMathOperation.resultOf('+', left, right));
System.out.println(RegularMathOperation.resultOf('-', left, right));
System.out.println(RegularMathOperation.valueOf('*').calculate(left, right));
System.out.println(RegularMathOperation.valueOf('*').calculate( left, right));
System.out.println(RegularMathOperation.DIVIDE.calculate(left, right));
System.out.println(RegularMathOperation.DIVIDE.calculate(right,left));
}
}