Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.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如何让两个变量指向同一个原始对象_Java_Pointers_Variables - Fatal编程技术网

Java如何让两个变量指向同一个原始对象

Java如何让两个变量指向同一个原始对象,java,pointers,variables,Java,Pointers,Variables,我正在用Java编写一个平台游戏,其中有各种各样的游戏对象,比如平台、玩家和(最终)敌人。我用矩形描述这些游戏对象的位置:x位置、y位置、宽度和高度。但是,我还想添加其他变量来描述位置:左、上、右和下。对于最后两个,我知道每当x、y、width或height被修改时,我都需要更改它们,但是由于left和top与x和y相同,我想知道如何让它们指向与x和y相同的值。我认为这可以在C中通过#define函数实现,但遗憾的是这是Java,而不是C 如何让两个不同的变量名指向Java中的同一个值,这样,如

我正在用Java编写一个平台游戏,其中有各种各样的游戏对象,比如平台、玩家和(最终)敌人。我用矩形描述这些游戏对象的位置:x位置、y位置、宽度和高度。但是,我还想添加其他变量来描述位置:左、上、右和下。对于最后两个,我知道每当x、y、width或height被修改时,我都需要更改它们,但是由于left和top与x和y相同,我想知道如何让它们指向与x和y相同的值。我认为这可以在C中通过#define函数实现,但遗憾的是这是Java,而不是C

如何让两个不同的变量名指向Java中的同一个值,这样,如果一个变量名发生更改,另一个变量名也会发生更改

编辑:以下是我的GameObject类的基础知识:

 public abstract class GameObject {
    protected float x;
    protected float y;

    protected float right;
    protected float bottom;
    protected float width;
    protected float height;

    public float getX() {
        return x;
    }

    public float getY() {
        return y;
    }

    public void setX(float x) {
        this.x = x;
        right=this.x+width;
    }

    public void setY(float y) {
        this.y = y;
        bottom=this.y+height;
    }
    //A bunch of other getters/setters

    //Subclasses must have render (for displaying graphics) and tick (mainly for updating position) functions.
    public abstract void render(Graphics g);

    public abstract void tick();
}
我想做的是再添加两个变量:

protected float left = x;
protected float top = y;

并让它们引用与x和y相同的基元(而不是复制基元)。这显然是不可能的。谢谢你的回答

int/integer在java中是通过值传递的,而不是通过引用传递的

您可以定义一个新类,例如

public class Position {
     int x;
     int y;
}

让两个变量指向同一个位置实例。

int/integer在java中是通过值传递的,而不是通过引用传递的

您可以定义一个新类,例如

public class Position {
     int x;
     int y;
}

让两个变量指向同一个位置实例。

这可以通过创建一个新类轻松完成。Java是面向对象的,所以,基本上,一切都与类及其实例有关。请看下面的一个示例:

class GameObject
{
     //Instance variables
     //(all objects of this class will have their own)
     int x;
     int y;
     int width;
     int height;

     //Constructor
     //(you call a constructor when creating a new object)
     GameObject(int x, int y, int width, int height)
     {
          //Here you are assigning the values received to the instance variables (marked with "this", that represents the current object)
          this.x = x;
          this.y = y;
          this.width = width;
          this.height = height;
     }

    //This method will return the ordinate ("y") of the object's top position (assuming the "y axis" points down)
    int getTop() {
        return (y - height/2);
    }
}
创建类GameObject后,您可以创建它的实例(对象)。请看下面:

GameObject player = new GameObject(0, 0, 50, 50); //creates a new object with coordinates (0,0), width = 50 and height = 50
GameObject enemy1 = new GameObject(30, 50, 100, 100);

//Getting the "x" position of the player and the monster
System.out.println("Player X = " + player.x);
System.out.println("Enemy1 X = " + enemy1.x);

//Get the top position "y" 
System.out.println("Player top = " + player.getTop());

//Change player's "y"
player.y = 10;

//Get player's top position again
System.out.println("Player top = " + player.getTop()); //it will have changed, since "y" was changed.

Oracle在面向对象编程方面有很好的一课,你应该去看看。C语言是结构化的,而Java是面向对象的,这是两种不同的范例。我希望这能有所帮助。干杯

这可以通过创建一个新类轻松完成。Java是面向对象的,所以,基本上,一切都与类及其实例有关。请看下面的一个示例:

class GameObject
{
     //Instance variables
     //(all objects of this class will have their own)
     int x;
     int y;
     int width;
     int height;

     //Constructor
     //(you call a constructor when creating a new object)
     GameObject(int x, int y, int width, int height)
     {
          //Here you are assigning the values received to the instance variables (marked with "this", that represents the current object)
          this.x = x;
          this.y = y;
          this.width = width;
          this.height = height;
     }

    //This method will return the ordinate ("y") of the object's top position (assuming the "y axis" points down)
    int getTop() {
        return (y - height/2);
    }
}
创建类GameObject后,您可以创建它的实例(对象)。请看下面:

GameObject player = new GameObject(0, 0, 50, 50); //creates a new object with coordinates (0,0), width = 50 and height = 50
GameObject enemy1 = new GameObject(30, 50, 100, 100);

//Getting the "x" position of the player and the monster
System.out.println("Player X = " + player.x);
System.out.println("Enemy1 X = " + enemy1.x);

//Get the top position "y" 
System.out.println("Player top = " + player.getTop());

//Change player's "y"
player.y = 10;

//Get player's top position again
System.out.println("Player top = " + player.getTop()); //it will have changed, since "y" was changed.

Oracle在面向对象编程方面有很好的一课,你应该去看看。C语言是结构化的,而Java是面向对象的,这是两种不同的范例。我希望这能有所帮助。干杯

在java中有两种类型的变量。 1.基本类型 2.参考类型

如果希望通过不同的变量引用同一对象,则需要使用引用类型变量

在游戏应用程序中,这些参数可能会频繁更改。 因此如果所有时间值、left和top都与x和y相同,则可以使用一个变量来表示这两个参数,从而节省内存。 e、 g:对于left和x,使用一个变量

或者使用java OOP概念, 您可以将这些x、y、left和top参数设置为私有,并为它们分配getter和setter

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

public setLeft(int left){
    this.left = left;
}

public int setY(int y){
    this.y =y;
}

public setTop(int top){
    this.top= top;
}

public setXnLeft(int xnleft){
    setX(xnleft);
    setleft(xnleft);
}

public setYnTop(int yntop){
    setY(yntop);
    setTop(yntop);
}

在java中有两种类型的变量。 1.基本类型 2.参考类型

如果希望通过不同的变量引用同一对象,则需要使用引用类型变量

在游戏应用程序中,这些参数可能会频繁更改。 因此如果所有时间值、left和top都与x和y相同,则可以使用一个变量来表示这两个参数,从而节省内存。 e、 g:对于left和x,使用一个变量

或者使用java OOP概念, 您可以将这些x、y、left和top参数设置为私有,并为它们分配getter和setter

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

public setLeft(int left){
    this.left = left;
}

public int setY(int y){
    this.y =y;
}

public setTop(int top){
    this.top= top;
}

public setXnLeft(int xnleft){
    setX(xnleft);
    setleft(xnleft);
}

public setYnTop(int yntop){
    setY(yntop);
    setTop(yntop);
}

这不能在Java中完成,因为您是认真的。您可以有两个指向同一对象的对象引用,但是如果您有一个普通的基元变量,那么它们总是不同的,并且不相互引用。您可以使一个对象保存一个基本体,对同一对象有两个不同的引用,通过一个引用对该对象内容的更改将反映在另一个引用中,但是您不能有
intx
inty
,因为写入
x=10
将使
y==10
,从您的意思来看,这在Java中是无法实现的。您可以有两个指向同一对象的对象引用,但是如果您有一个普通的基元变量,那么它们总是不同的,并且不相互引用。您可以使一个对象保存一个基本体,对同一对象有两个不同的引用,通过一个引用对该对象内容的更改将反映在另一个引用中,但是不能有
intx
inty
,写入
x=10
将使
y==10
,如果确实需要类似于对原语的引用的对象,则需要创建一个包含该原语值的对象,然后共享“holder”对象

然而,我不认为这是解决你问题的最好方法

据我所知,游戏对象完全可以用
x
y
高度
宽度
变量来描述<另一方面,代码>左侧、
顶部
右侧
底部
是可以从四个基本值派生的值

事实上,一个游戏对象可能只包含
x
y
高度
宽度
,并在需要时计算对象外部的其他四个值。这样的对象可以如下所示:

public class GameObject {
  private int x, y, height, width;

  public int getX() { return x; }
  public int getY() { return y; }
  public int getHeight() { return height; }
  public int getWidth() { return width; }

  public void setX(int x) { this.x = x; }
  public void setY(int y) { this.y = y; }
  public void setHeight(int height) { this.height = height; }
  public void setwidth(int width) { this.width = width; }
}
请注意,实际变量是私有的,只能通过getter读取。这有助于避免以后使用变量引用

现在,使用一种简单而独特的方式访问
左侧
顶部
ri