java - Java - 如何编写value()方法?

58 3

ArithmeticOperation:ArithmeticOperation表示两个值之间的算术运算,ArithmeticOperation类型应包含以下enum类型,以表示语言可能的操作:

public enum Operator { Add ,sub ,mult ,div ,Rem ; } 运算符表示 -* /和%操作,


public interface Operation {


 public enum Operator { Add, Sub, Mult, Div, Rem; }


}



import java.util.*;


import java.io.*;



public class ArithmeticOperation <I> implements Operation{


 ArithmeticOperation <Integer> leftoperand = new 


ArithmeticOperation <Integer>();


 ArithmeticOperation <Operator> operation = new 


ArithmeticOperation <Operator>();


 ArithmeticOperation <Integer> rightoperand = new 


ArithmeticOperation <Integer>(); 



 public Integer value(State s){


 return leftoperand+operation+rightoperand;


 }



 public String toString(){


 return"("+leftoperand+" ,"+operation+", 


"+rightoperand+")";



 } 



}



1 error found:


File: /Users/a13875810600/Desktop/ArithmeticOperation.java [line: 


11]


Error: bad operand types for binary operator '+'


 first type: ArithmeticOperation<java.lang.Integer>


 second type: ArithmeticOperation<Operation.Operator>



我想知道如何添加整数类型和运算符类型。

时间: 原作者:

137 4

我想那是:


public Integer value(State s){


 switch (operation) {


 case Add:


 return leftoperand.value(s) + rightoperand.value(s);


 case Sub:


 return leftoperand.value(s) - rightoperand.value(s);


 case Mul:


 return leftoperand.value(s) * rightoperand.value(s);


 case Div:


 return leftoperand.value(s) / rightoperand.value(s);


 case Rem:


 return leftoperand.value(s) % rightoperand.value(s);


 }


}



原作者:
95 0


public interface Operation {


 public enum Operator { Add, Sub, Mult, Div, Rem; }


}



import java.util.*;


import java.io.*;



public class ArithmeticOperation<I> implements Operation {



 ArithmeticOperation <Integer> leftoperand = new ArithmeticOperation <Integer>();


 ArithmeticOperation <Operator> operation = new ArithmeticOperation <Operator>();


 ArithmeticOperation <Integer> rightoperand = new ArithmeticOperation <Integer>(); 



 public Integer value (State state) {


 switch (operation) {


 case Add:


 return leftoperand.value(state) + rightoperand.value(state);


 case Sub:


 return leftoperand.value(state) - rightoperand.value(state);


 case Mult:


 return leftoperand.value(state) * rightoperand.value(state);


 case Div:


 return leftoperand.value(state) / rightoperand.value(state);


 case Rem:


 return leftoperand.value(state) % rightoperand.value(state);


 }


 }



 public String toString() {


 return"(" + leftoperand +"," + operation +"," + rightoperand +")";


 }


}



原作者:
...