-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对象,但是我真的说它要共享原语。 –