【问题标题】:Java setter does not change the value of an integer [closed]Java setter不会更改整数的值[关闭]
【发布时间】:2020-02-10 11:24:20
【问题描述】:

编辑:添加了 MovementDataStorage 数据 = new MovementDataStorage();到 cmets 中指出的主类进行澄清。

我有 3 个类,都在同一个包中。 Main 类中 main 方法的代码 sn-p:

ActionsMovement move = new ActionsMovement();
MovementDataStorage data = new MovementDataStorage();

move.goForward();
System.out.println(data.getLocationNorth()); //this would show 0, intended result is 1

我的 ActionsMovement 类具有以下 sn-p:

MovementDataStorage data = new MovementDataStorage();

public void goForward()
{
      if (data.getDirection().equals("North")) {
            data.setLocationNorth(data.getLocationNorth() + 1);
    }
}

最后,我的 MovementDataStorage 有以下代码 sn-p:

private int locationNorth;
private String direction = "North";

public int getLocationNorth() {
        return locationNorth;
    }

    public void setLocationNorth(int locationNorth) {
        this.locationNorth = locationNorth;
    }

    public String getDirection() {
        return direction;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }

move.goForward(); 运行时,int locationNorth 的值不会增加 - 我尝试从 main 方法和 goForward 方法内部检查值。

如果我手动更改int locationNorth 值,我可以看到更改。如果我通过move.goForward(); 进行操作,它似乎不会改变。

如果在我的main 方法中添加:

data.setLocationNorth(data.getLocationNorth()+1);

System.out.println(data.getLocationNorth());

int locationNorth 的值确实变成了我想要的值。

代码运行和编译没有错误/异常

【问题讨论】:

  • 这是调试器派上用场的地方...
  • 简短建议:locationNorth 未在您的班级中初始化。
  • @Andrea 这就是它以默认值 0 开头的原因
  • @Kleronomas 的值可能不等于“North”

标签: java private getter setter


【解决方案1】:

问题是您有两个MovementDataStorage,一个在您打印的Main 类中,另一个在您设置其值的ActionsMovement 中。

一种解决方案是使用来自ActionsMovementMovementDataStorage

class Main {
    ActionsMovement move = new ActionsMovement();
    move.goForward();
    System.out.println(move.getData().getLocationNorth());
}

class ActionsMovement {

    public MovementDataStorage getData() {
        return this.data;
    }
}

如果您需要 MovementDataStorage 在 main 中,您可以创建一个实例并将其作为参数发送

class Main {
    MovementDataStorage data = new MovementDataStorage();
    ActionsMovement move = new ActionsMovement(data);

    move.goForward();
    System.out.println(move.getData().getLocationNorth());
}

class ActionsMovement {

    MovementDataStorage data;

    public ActionsMovement(MovementDataStorage data) {
        this.data = data;
    }

    public ActionsMovement() {
        this.data = new MovementDataStorage();
    }

    public MovementDataStorage getData() {
        return this.data;
    }
}

【讨论】:

    【解决方案2】:

    表达式

    if (data.getDirection().equals("North"))
    

    显然没有评估为真。我的猜测是它要么未正确初始化,要么具有不同的值。

    【讨论】:

      【解决方案3】:

      所以您的ActionsMovement 已经包含MovementDataStorage,因此无需在您的 main 方法中重新创建该对象。

      因此,在您的 ActionsMovement 类中,您可能应该有一个函数来检索 MovementDataStorage,如下所示:

      public getMovementDataStorage() {
          return this.data;
      }
      

      然后从你的 main 你可以做这样的事情:

      ActionsMovement move = new ActionsMovement();
      
      move.goForward();
      System.out.println(data.getMovementDataStorage().getLocationNorth()); 
      

      【讨论】:

        猜你喜欢
        • 2013-02-23
        • 2022-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-16
        • 2019-08-07
        • 1970-01-01
        • 2012-12-04
        相关资源
        最近更新 更多