我知道如何把價值從一類到另一個利用價值。 就在這裏它不起作用。我不明白爲什麼。不能在不同的班級
請幫我找到一個bug。
當我試圖讓值:
c = new CartesianCoordinate(x, y);
x = c.getX();
它始終是零。
public class Velocity
{
// instance variables - replace the example below with your own
private double x;
private double y;
/**
* Constructor for objects of class Velocity
*/
public Velocity(CartesianCoordinate c)
{
// initialise instance variables
c = new CartesianCoordinate(x,y);
x = c.getX();
System.out.println(c.getX());
System.out.println(c.getY());
System.out.println(c.x);
}
public double getX()
{
return x;
}
這裏是我的CartesianCoordinate:
public class CartesianCoordinate
{
// instance variables - replace the example below with your own
public double x;
public double y;
/**
* Constructor for objects of class CartesianCoordinate
*/
public CartesianCoordinate(double x, double y)
{
// initialise instance variables
this.x = x;
this.y = y;
}
public void setX(double x)
{
// put your code here
this.x = x;
}
public void setY(double y)
{
this.y = y;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
}
行'C =新CartesianCoordinate(X,Y);'陰影你傳遞的參數 - 這是你的意圖? – jedwards 2013-03-22 19:17:49
在您給出的示例代碼中,沒有值被分配給構造函數調用'new CartesianCoordinate(x,y)'的x和y。備註:CartesianCoordinate中的實例變量應該是私有的。 – tbsalling 2013-03-22 19:21:55