简体   繁体   中英

How do you change the value of a getter method that is within an entity?

I'm trying to incriment the value of a number by 1.

public void move(){
    for (Entity e:entities){ 
           e.getX()+1;
      }

You need to defined a setter in your Entity class.

public void setX(int x)
{
    this.x = x;
}

Then you can do

public void move()
{
    for(Entity e:entities)
        e.setX(e.getX() + 1);
}

You can't mutate the value from the getter directly, as it return the value but not the reference.

You can't change a property that way. You must use a setter:

public void move() {
  for (Entity e: entities){
    e.setX(e.getX()+1);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM