Android变量之间的联系变得不可逆转?

Android变量之间的联系变得不可逆转?,android,Android,好吧,这一个很奇怪-我需要两个矩形作为Healthbar(边框和颜色填充) 当我计算第一个矩形(填充)时,我将该值指定给我的条形矩形-但在运行时,当填充矩形被修改时,条形也是-只有通过手动设置条形矩形的参数,我才能保持其形状-这怎么可能 通过简单的赋值,变量可以永远链接起来吗- 以下是完整的代码: public class HealthBar { public int MaxValue; int CurrentValue; public int TargetValue; int marg

好吧,这一个很奇怪-我需要两个矩形作为Healthbar(边框和颜色填充)

当我计算第一个矩形(填充)时,我将该值指定给我的条形矩形-但在运行时,当填充矩形被修改时,条形也是-只有通过手动设置条形矩形的参数,我才能保持其形状-这怎么可能

通过简单的赋值,变量可以永远链接起来吗-

以下是完整的代码:

  public class HealthBar {

public int MaxValue;
int CurrentValue;
public int TargetValue;

int margin = 10;
Rect Rectangle;
private  Rect BarRectangle;
int screenWidth;

boolean IsPlayer1;

 Paint paint = new Paint(); 

 int Green;
 int Yellow;
 int Red;

 int opacity = 196;

public HealthBar(boolean isPlayer1, DeviceProperties device, int maxvalue) {

    paint.setARGB(196, 0, 255, 0);
    paint.setStyle(Paint.Style.FILL);

Green = Color.argb(opacity, 0, 255, 128);
Yellow = Color.argb(opacity, 255, 255, 128);
Red = Color.argb(opacity, 255, 0, 128);


MaxValue = maxvalue;
CurrentValue = MaxValue;
TargetValue = MaxValue;


    IsPlayer1 = isPlayer1;

    screenWidth = device.Screen.width();

    if(IsPlayer1) {



        Rectangle = new Rect(
                             margin,                    //x
                             device.Screen.height() - 14, //y
                             margin + MaxValue,           //width
                             device.Screen.height() - 2  //height
                             );



    } else {



        Rectangle = new Rect(
                             device.Screen.width() - margin - MaxValue,                     //x
                             device.Screen.height() - 14, //y
                             device.Screen.width() - margin,          //width
                             device.Screen.height() - 2  //height
                             );




    }


//Assign Bar Rectangle to Rectangle
    BarRectangle = Rectangle;
}


public void Damage(int amount)
{
    TargetValue = CurrentValue - amount;
}

public void Update() 
{

    if (CurrentValue > TargetValue)
    {
        CurrentValue -= 1;

        if (IsPlayer1) 
            Rectangle.right = margin + CurrentValue;
        else
            Rectangle.left = screenWidth - margin - CurrentValue;
    }

    if (TargetValue <= MaxValue * 0.33) {
        paint.setColor(Red);
    } else if (TargetValue <= MaxValue * 0.66) {
        paint.setColor(Yellow);
    } else {
        paint.setColor(Green);
    }


}


public void Draw(ResourceManager rm, Canvas c, Paint p) 
{



    c.drawRect(Rectangle, paint);
   c.drawBitmap(rm.getBitmap("healthbar"), null, BarRectangle, p);

}


    }//End HealthBar
有人能解释一下,在构造函数中分配一个变量如何使它在另一个变量更新时更新它的值吗


现在我遇到了一个可复制的问题——我想在分配字符位置时,以前也发生过这种情况,我使用一个自定义类来保存值(Vector2),每当其中一个更新时,另一个也会更新。

变量
矩形
BarRectangle
仅引用对象。这意味着通过代码中的“简单赋值”

//Assign Bar Rectangle to Rectangle
BarRectangle = Rectangle;
您不复制内容(即
Rect
的字段),只复制其引用。更改其中一个字段会在这两个字段中反映自身,因为它们现在引用同一个实例

为了创建具有相同内容(因此不共享引用)的新实例,可以使用以下构造函数:

BarRectangle = new Rect(Rectangle);

我通过手动指定参数来修复它,例如://Assign Bar Rectangle to Rectangle BarRectangle.left=Rectangle.left;BarRectangle.right=矩形.right;BarRectangle.top=矩形.top;BarRectangle.bottom=矩形.bottom;或者在变量定义public Rect Rect Rectangle=New Rect()处调用New关键字;private Rect BarRectangle=new Rect();不确定哪一个--我将来如何避免这种情况?我的意思是,为什么intA=intB工作而RectA=RectB不工作?谢谢-所以基本上如果你使用的是有构造函数的东西-使用它?正如我在回答中所写的那样,变量
RectX
包含一个对象引用(而不是对象字段的直接值),而
intX
(假设它们是
int
s)包含一个值(因此,
int
s称为基元)。
BarRectangle = new Rect(Rectangle);