有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使两个类在一个接口中协同工作

我对接口方法相当陌生

我正试图使加法和乘法类在一个基于我所看到的练习的接口中协同工作

对于类Addition,我必须使用IntegerExpression的两个实例,+操作的左侧和右侧参数。我必须使用两个实例变量来存储对这两个子表达式的引用。getValue应该返回两个子表达式提供的值相加的结果

对于另一个类乘法,getValue()应该通过将两个子表达式提供的值相乘来返回结果

我创建了3个使用IntegerExpression的类,但是我的代码导致编译错误,这就是产生这个问题的原因:

public interface IntegerExpression {
    int getValue();
}


public class IntegerConstant implements IntegerExpression {
private int val;
public int getValue(){
    return val;
}  
public int IntegerConstant(int num){
    val = num;
    return val;
    }
}


public class Addition implements IntegerExpression {
private int numberOne; 
private int numberTwo;
public Addition(int one, int two)
{
    int result = numberOne+numberTwo;
    return result;
}
public int getValue(){
    return Addition(int numberOne, int numberTwo);
    }
}


public class Multiplication implements IntegerExpression{
    public int getValue(){
        return numberOne*numberTwo;
     }
}

假设我能够修复上述代码-我希望能够编写以下代码:

IntegerExpression p1 = new Addition(new IntegerConstant(1), new IntegerConstant(2));

IntegerExpression p2 = new Multiplication(p1, new IntegerConstant(3));

共 (2) 个答案

  1. # 1 楼答案

    在OP中提供的代码中有几个问题,我冒昧地重构代码如下(使用following Java IDE验证):

    public interface IntegerExpression {
        int getValue();
    }
    
    public abstract class TwoOperandOperator implements IntegerExpression {
       protected IntegerExpression leftNum; 
       protected IntegerExpression rightNum;
       protected TwoOperandOperator(IntegerExpression leftNum, IntegerExpression rightNum) {
          this.leftNum = leftNum;
          this.rightNum = leftNum;
       }
    }
    
    public class IntegerConstant implements IntegerExpression {
       private int val;
       @Override
       public int getValue(){
          return val;
       }  
       public /*int*/ IntegerConstant(int val){
          this.val = val;
          // No need for the code below - constructors do not return values 
          // return val;
       } 
    }
    
    
    public class Addition extends TwoOperandOperator {
       public Addition(IntegerExpression leftNum, IntegerExpression rightNum)
       {
          super(leftNum,rightNum);
       }
       @Override
       public int getValue(){
          return (leftNum.getValue() + rightNum.getValue());
       }
    }
    
    
    public class Multiplication extends TwoOperandOperator {
       public Multiplication (IntegerExpression leftNum, IntegerExpression rightNum)
       {
          super(leftNum,rightNum);
       }
       @Override
       public int getValue(){
            return (leftNum.getValue() * rightNum.getValue());
       }
    }
    

    我建议您对两个基于操作数的整数常量使用抽象实现,如上所述-构造代码重复出现

  2. # 2 楼答案

    此外,您已在构造函数中返回sum。构造函数通常用于初始化类的变量。求和应该在getValue方法本身中完成

    public class Addition implements IntegerExpression {
        private int numberOne; 
        private int numberTwo;
        public Addition(int one, int two) {
            this.numberOne = one;
            this.numberTwo = two;
        }
        public int getValue(){
            int result = this.numberOne+this.numberTwo;
            return result;
        }
    }
    

    }