Question

Ok so I need to get the variables from one class's method and update it in another class.

Class which updates method.

public abstract class MovableObject{
    protected int speed;
    protected int heading;

    public void move(){
        setX(finalX);
        setY(finalY);
    }

Class which needs updating:

public class Car extends MoveableObject{
    private int height;
    private int width;

    public Car(){
        super.setX(200);
        super.setY(2);
    }

I have an iterator that goes through a list and checks the X and Y coordinates, currently it keeps printing out (200, 2) constantly but the Car is moving. So the class MovableObject has the updated coordinates but since I'm calling it from Car it's not getting the right coordinates to the moving car. I need to pass the variables from move() to more than one class also, that should be the same since the Iterator takes care of what is updated right or is this way very complicated?

listObject = new GameObjectCollection();
car = new Car();
listObject.add(car);

System.out.println("CAR: " + ((Car)gameObj).getX() + " " + ((Car)gameObj).getY());
Était-ce utile?

La solution

That constructor of Car class initialize those values so if you create a Car it will have by default those values.

Solution 1: Create another constructor

public class Car extends MoveableObject{
    private int height;
    private int width;

    public Car(){
        super.setX(200);
        super.setY(2);
    }

    public Car(int x, int y){
       super.setX(x);
       super.setY(y);
    } 

}

If you use the second constructor you can define the values you want for that instance of Car.

Car c = new Car(100, 3);

This really does:

 public Car(int x, int y){
           super(); //Call the creation of the superclass
           super.setX(x); //Modify superclass X and Y values.
           super.setY(y);
        } 

Solution 2: Use getters and setter methods (provide they are accesible and they are in Class or Superclasses)

Once you create a Car you can change the x and y values before adding them to the list.

Car c = new Car();
c.setX(100);
c.setY(3);

Autres conseils

You want to build a get method into your class (usually you also want a set method for anything you want the outside world to be able to change).

something like:

 public int getx(){
      return this.x;  // where x is the variable you are returning
 }

because this is public, other classes can access it. If you keep your variables private, then no outside sources can access or update them except by the public methods you define.

Similarly, if you wanted outside sources to be able to update this, then you could define a setter method:

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

If x is private, then outside sources can only modify it through this method (or other methods you define for the class).

With these methods, you could do something like:

 SomeClass example = new SomeClass();
 example.setx(10);  // sets example's x variable to 10
 System.out.println(example.getx());  // will print 10
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top