Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从另一个类';s法_Java_Variables_Coordinates - Fatal编程技术网

Java 如何从另一个类';s法

Java 如何从另一个类';s法,java,variables,coordinates,Java,Variables,Coordinates,我需要从一个类的方法中获取变量,然后在另一个类中更新它 类,该类更新方法 public abstract class MovableObject{ protected int speed; protected int heading; public void move(){ setX(finalX); setY(finalY); } 需要更新的类: public class Car extends MoveableObject{

我需要从一个类的方法中获取变量,然后在另一个类中更新它

类,该类更新方法

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

    public void move(){
        setX(finalX);
        setY(finalY);
    }
需要更新的类:

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

    public Car(){
        super.setX(200);
        super.setY(2);
    }
我有一个迭代器,它遍历一个列表并检查X和Y坐标,目前它不断打印(200,2),但汽车正在移动。所以类MovableObject有更新的坐标,但是因为我从Car调用它,所以它没有得到移动汽车的正确坐标。我还需要将变量从
move()
传递到多个类,这应该是相同的,因为迭代器负责正确更新的内容,或者这种方式非常复杂

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

System.out.println("CAR: " + ((Car)gameObj).getX() + " " + ((Car)gameObj).getY());

您希望在类中构建一个get方法(通常您还希望为希望外部世界能够更改的任何内容提供一个set方法)

比如:

 public int getx(){
      return this.x;  // where x is the variable you are returning
 }
 SomeClass example = new SomeClass();
 example.setx(10);  // sets example's x variable to 10
 System.out.println(example.getx());  // will print 10
因为它是公共的,所以其他类可以访问它。如果您将变量保持私有,那么除了您定义的公共方法外,任何外部源都无法访问或更新它们

类似地,如果您希望外部源能够更新它,那么您可以定义一个setter方法:

 public void setx(int input){
      this.x = input;
 }
如果x是私有的,则外部源只能通过此方法(或为类定义的其他方法)修改它

使用这些方法,您可以执行以下操作:

 public int getx(){
      return this.x;  // where x is the variable you are returning
 }
 SomeClass example = new SomeClass();
 example.setx(10);  // sets example's x variable to 10
 System.out.println(example.getx());  // will print 10

您希望在类中构建一个get方法(通常您还希望为希望外部世界能够更改的任何内容提供一个set方法)

比如:

 public int getx(){
      return this.x;  // where x is the variable you are returning
 }
 SomeClass example = new SomeClass();
 example.setx(10);  // sets example's x variable to 10
 System.out.println(example.getx());  // will print 10
因为它是公共的,所以其他类可以访问它。如果您将变量保持私有,那么除了您定义的公共方法外,任何外部源都无法访问或更新它们

类似地,如果您希望外部源能够更新它,那么您可以定义一个setter方法:

 public void setx(int input){
      this.x = input;
 }
如果x是私有的,则外部源只能通过此方法(或为类定义的其他方法)修改它

使用这些方法,您可以执行以下操作:

 public int getx(){
      return this.x;  // where x is the variable you are returning
 }
 SomeClass example = new SomeClass();
 example.setx(10);  // sets example's x variable to 10
 System.out.println(example.getx());  // will print 10

您希望在类中构建一个get方法(通常您还希望为希望外部世界能够更改的任何内容提供一个set方法)

比如:

 public int getx(){
      return this.x;  // where x is the variable you are returning
 }
 SomeClass example = new SomeClass();
 example.setx(10);  // sets example's x variable to 10
 System.out.println(example.getx());  // will print 10
因为它是公共的,所以其他类可以访问它。如果您将变量保持私有,那么除了您定义的公共方法外,任何外部源都无法访问或更新它们

类似地,如果您希望外部源能够更新它,那么您可以定义一个setter方法:

 public void setx(int input){
      this.x = input;
 }
如果x是私有的,则外部源只能通过此方法(或为类定义的其他方法)修改它

使用这些方法,您可以执行以下操作:

 public int getx(){
      return this.x;  // where x is the variable you are returning
 }
 SomeClass example = new SomeClass();
 example.setx(10);  // sets example's x variable to 10
 System.out.println(example.getx());  // will print 10

您希望在类中构建一个get方法(通常您还希望为希望外部世界能够更改的任何内容提供一个set方法)

比如:

 public int getx(){
      return this.x;  // where x is the variable you are returning
 }
 SomeClass example = new SomeClass();
 example.setx(10);  // sets example's x variable to 10
 System.out.println(example.getx());  // will print 10
因为它是公共的,所以其他类可以访问它。如果您将变量保持私有,那么除了您定义的公共方法外,任何外部源都无法访问或更新它们

类似地,如果您希望外部源能够更新它,那么您可以定义一个setter方法:

 public void setx(int input){
      this.x = input;
 }
如果x是私有的,则外部源只能通过此方法(或为类定义的其他方法)修改它

使用这些方法,您可以执行以下操作:

 public int getx(){
      return this.x;  // where x is the variable you are returning
 }
 SomeClass example = new SomeClass();
 example.setx(10);  // sets example's x variable to 10
 System.out.println(example.getx());  // will print 10

Car
类的构造函数初始化这些值,因此,如果创建
Car
,默认情况下它将具有这些值

解决方案1:创建另一个构造函数

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);
    } 

}
如果使用第二个构造函数,则可以为
Car
的实例定义所需的值

Car c = new Car(100, 3);
这确实是:

 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);
        } 
解决方案2:使用getter和setter方法(前提是它们是可访问的,并且在类或超类中)

创建
Car
后,可以在将
x
y
值添加到列表之前更改它们

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

Car
类的构造函数初始化这些值,因此,如果创建
Car
,默认情况下它将具有这些值

解决方案1:创建另一个构造函数

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);
    } 

}
如果使用第二个构造函数,则可以为
Car
的实例定义所需的值

Car c = new Car(100, 3);
这确实是:

 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);
        } 
解决方案2:使用getter和setter方法(前提是它们是可访问的,并且在类或超类中)

创建
Car
后,可以在将
x
y
值添加到列表之前更改它们

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

Car
类的构造函数初始化这些值,因此,如果创建
Car
,默认情况下它将具有这些值

解决方案1:创建另一个构造函数

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);
    } 

}
如果使用第二个构造函数,则可以为
Car
的实例定义所需的值

Car c = new Car(100, 3);
这确实是:

 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);
        } 
解决方案2:使用getter和setter方法(前提是它们是可访问的,并且在类或超类中)

创建
Car
后,可以在将
x
y
值添加到列表之前更改它们

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

Car
类的构造函数初始化这些值,因此,如果创建
Car
,默认情况下它将具有这些值

解决方案1:创建另一个构造函数

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);
    } 

}
如果使用第二个构造函数,则可以为
Car
的实例定义所需的值

Car c = new Car(100, 3);
这确实是:

 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);
        } 
解决方案2:使用getter和setter方法(前提是它们是可访问的,并且在类或超类中)

创建
Car
后,可以在将
x
y
值添加到列表之前更改它们

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

如何创建汽车列表?如何打印列表项的值?请发布代码。不确定你到底想要什么,但那是