-1
我已創建類,較2-d點一類,和表示線段,其存儲兩個2 d點的類。但是,當我創建了三個單獨的線段並在其中一個線段中更改值時,使用LineSegment類中的函數setStartPoint(Point2D arg),它會更改它們中的所有值。我假設這是因爲我爲每個LineSegment創建的所有對象都指向同一個對象,但是在我看來,我每次都聲明一個新對象(使用new?)。如果有人能夠發現我的錯誤,並指出我的方向正確,我將非常感激。更改一個對象改變所有這些
請注意,我只是需要一個例子來說明我做錯了什麼,我不想爲我應該改變的代碼行或者它在哪裏的確切答案,只是想知道我是什麼沒有正確實施,就像我的計算機科學實驗室一樣。
驅動程序類,教授
public class driver
{
public static void lineSegmentDriver()
{
LineSegment a = new LineSegment();
LineSegment b = new LineSegment(new Point2D(1,1), new Point2D(2,2));
LineSegment c = new LineSegment(b);
a.setStartPoint(new Point2D(3,3));
a.setEndPoint(new Point2D(4,4));
System.out.println("Line a: " + a.toString());
System.out.println("Line b: " + b.toString());
System.out.println("Line c: " + c.toString());
System.out.println("Line b's distance between points: " + b.distance());
System.out.println("Does a equal b? " + a.equals(b));
System.out.println("Does a equal c? " + a.equals(c));
System.out.println("Does b equal c? " + b.equals(c));
}
}
線段類,我寫的書面(註釋去掉)
public class LineSegment
{
private Point2D p1;
private Point2D p2;
public LineSegment()
{
this.p1 = new Point2D(0,0);
this.p2 = new Point2D(0,0);
}
public LineSegment(Point2D p1, Point2D p2)
{
if (p1 == null || p2 == null)
{
this.p1 = new Point2D(0,0);
this.p2 = new Point2D(0,0);
}
else
{
this.p1 = new Point2D(p1);
this.p2 = new Point2D(p2);
}
}
public LineSegment(LineSegment l)
{
this.p1 = new Point2D(l.p1);
this.p2 = new Point2D(l.p2);
}
public Point2D getStartPoint()
{
Point2D varRet = new Point2D(p1);
return varRet;
}
public void setStartPoint(Point2D arg)
{
this.p1 = new Point2D(arg);
}
public Point2D getEndPoint()
{
Point2D varRet = new Point2D(p2);
return varRet;
}
public void setEndPoint(Point2D arg)
{
this.p2 = new Point2D(arg);
}
public String toString()
{
return "Line start (" + this.p1.getX() + "," + this.p1.getY() + ") and end (" +
this.p2.getX() + "," + this.p2.getY() + ")";
}
public boolean equals(Object o)
{
if (o instanceof LineSegment && o != null)
{
LineSegment toCompare = (LineSegment) o;
if (toCompare.p1.equals(this.p1) &&
toCompare.p2.equals(this.p2))
return true;
}
return false;
}
public double distance()
{
return Math.sqrt(((this.p1.getY() - this.p2.getY()) * (this.p1.getY() - this.p2.getY())) +
((this.p2.getX() - this.p1.getX()) * (this.p1.getX() - this.p2.getX())));
}
}
Point2D類,由我
public class Point2D
{
private static int x;
private static int y;
public Point2D()
{
this.x = -1;
this.y = -1;
}
public Point2D(int x, int y)
{
setX(x);
setY(y);
}
public Point2D(Point2D point)
{
setX(point.x);
setY(point.y);
}
public void setX(int x)
{
this.x = x;
}
public int getX()
{
return this.x;
}
public void setY(int y)
{
this.y = y;
}
public int getY()
{
return this.y;
}
public void resetToOrigin()
{
this.x = 0;
this.y = 0;
}
public void translate(int x, int y)
{
this.x += x;
this.y += y;
}
@Override
public String toString()
{
return Integer.toString(this.x) + "," + Integer.toString(this.y);
}
public boolean equals(Point2D pointObject)
{
if (this.x == pointObject.getX() &&
this.y == pointObject.getY())
return true;
return false;
}
}
寫
Ughhh謝謝。不敢相信我沒有早點發現,我想不知怎麼,我的Java開發工具包或環境被搞亂了,並且在內存的同一個位置聲明瞭新的Point2D對象,但是我真的說它要共享原語。 –