[英]Java how to have two variables point to the same primitive object
我正在用 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 中完成。 你可以有兩個指向同一個對象的對象引用,但如果你有一個普通的原始變量,它們總是不同的並且不會相互引用。 您可以創建一個對象來保存一個原始對象,對同一個對象有兩個不同的引用,並且通過一個引用對該對象內容的更改將反映在另一個引用中,但是您不能有int x
和int y
其中寫x = 10
將使y == 10
。
如果您真的需要一些行為類似於對原始值的引用,則需要創建一個保存該原始值的對象,然后共享“持有者”對象。
但是,我認為這不是解決您問題的最佳方法。
據我了解您的游戲對象,它們可以完全用x
, y
, height
和width
變量來描述。 另一方面, left
、 top
、 right
和bottom
是可以從四個基本值派生的值。
事實上,可以有一個只包含x
、 y
、 height
和width
的游戲對象,並在需要時計算對象外的其他四個值。 這樣的對象可能如下所示:
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 讀取。 這有助於避免以后使用變量引用。
現在,要以一種簡單而獨特的方式訪問left
、 top
、 right
和bottom
,雖然您可以添加四個變量並使它們與已經存在的其他四個變量保持同步,但我認為這不是最好的方法.
我建議你在比平均水平稍微聰明一點的 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)
,這也將有所幫助,因為您只需要將更改應用於基本變量而不是從它們派生的值。
int/integer 是按值傳遞的,而不是在 java 中按引用傳遞。
您可以定義一個新類,例如,
public class Position {
int x;
int y;
}
讓兩個變量指向同一個 Position 實例。
這可以通過創建一個新類輕松完成。 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.
, you should check it out here . Oracle 有一堂關於的非常好的課程,您應該在這里查看。 C 語言是結構化的,而 Java 是面向對象的,這是兩種不同的范式。 我希望這有幫助。 干杯!
在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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.