【问题标题】:Java how to have two variables point to the same primitive objectJava如何让两个变量指向同一个原始对象
【发布时间】:2018-09-02 03:26:16
【问题描述】:

我正在用 Java 编写一个平台游戏,其中有各种游戏对象,例如平台、玩家和(最终)敌人。我用矩形来描述这些游戏对象的位置:x 位置、y 位置、宽度和高度。但是,我还想添加其他变量来描述位置:左、上、右和下。对于最后两个,我知道每当修改 x、y、宽度或高度时我都需要更改它们,但由于 left 和 top 与 x 和 y 相同,我想知道如何让它们指向相同值作为 x 和 y。我认为这可以通过#define 函数在 C 中完成,但遗憾的是这是 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 相同的原语(不复制原语)。显然,这似乎是不可能的。感谢您的回答!

【问题讨论】:

    标签: java pointers variables


    【解决方案1】:

    按照您的意思,这不能在 Java 中完成。你可以有两个指向同一个对象的对象引用,但是如果你有一个普通的原始变量,它们总是不同的并且不会相互引用。您可以制作一个对象来保存原语,对同一个对象有两个不同的引用,并且通过一个引用对该对象内容的更改将反映在另一个引用中,但是您 不能有一个int xint y,写x = 10会变成y == 10

    【讨论】:

      【解决方案2】:

      如果您确实需要类似于对原始数据的引用,您需要创建一个包含原始值的对象,然后共享“持有人”对象。

      但是,我认为这不是解决您的问题的最佳方法。

      据我了解,您的游戏对象完全可以用xyheightwidth 变量来描述。另一方面,lefttoprightbottom 是可以从四个基本值派生的值。

      事实上,可以让游戏对象只包含xyheightwidth,并在需要时在对象之外计算其他四个值。这样的对象可能如下所示:

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

      请注意,实际变量是 private 并且只能通过 getter 读取。这有助于避免以后使用变量引用。

      现在,有一种简单而独特的方式来访问 lefttoprightbottom,同时您可以添加四个变量并使它们与其他四个变量已经存在,我认为这不是最好的方法。

      我建议您在比平均水平稍高的 getter 中动态计算它们。它看起来像这样:

      public class GameObject {
        // getters and setters omitted for brevity
        private int x, y, height, width;
      
        public int getLeft() {
          return x;
        }
      
        public int getRight() {
          // assuming x increase toward the right
          return x + width;
        }
      
        public int getTop() {
          return y
        }
      
        public int getBottom() {
          // assuming y increase toward the bottom
          return y + height;
        }
      }
      

      如果您需要创建一些特殊的更新方法,例如moveRight(int delta)scale(double factor),这也会有所帮助,因为您只需要将更改应用到基本变量而不是从它们派生的值。

      【讨论】:

        【解决方案3】:

        int/integer 是按值传递的,而不是 java 中的引用。

        你可以定义一个新的类,例如,

        public class Position {
             int x;
             int y;
        }
        

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

        【讨论】:

          【解决方案4】:

          在java中有两种类型的变量。 1.原始类型 2.引用类型

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

          在您的游戏应用程序中,这些参数可能会经常更改。 所以如果所有时间值,left和top都相同与x和y,你可以使用一个变量来表示这两个参数,这样可以节省内存。 例如:对于 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);
          }
          

          【讨论】:

            【解决方案5】:

            这可以通过创建一个新类来轻松完成。 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 有一个关于面向对象编程的非常好的课程,你应该去看看here。 C 语言是结构化的,而 Java 是面向对象的,这是两种不同的范式。我希望这会有所帮助。干杯!

            【讨论】:

              猜你喜欢
              • 2017-01-20
              • 2010-10-11
              • 2018-12-15
              • 1970-01-01
              • 2019-11-02
              • 1970-01-01
              • 2012-08-22
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多