Understanding inheritance of class variables

后端 未结 2 729
无人共我
无人共我 2021-01-13 19:54

I\'m building a Challenge24Solver class in Java. The logic itself works and finds the solutions as would be expected (with an arbitrary number of arguments). A

2条回答
  •  囚心锁ツ
    2021-01-13 20:04

    Static variables are referenced via the class. You have defined operator as being of type Operation, so operator.token refers to Operation.token.

    I recommend using a getter for this purpose:

    abstract Operation { \\ Basic operation class
      abstract public double operate (double x, double y);
      abstract public String getToken();
    }
    
    class AddOp extends Operation {
      public double operate (double x, double y) {
        return x+y;
      }
      public String getToken() {
          return "+";
      }
    }
    

提交回复
热议问题