如何构建接收另一个点(x,y)并复制其值的复制构造函数?在Java中构建复制构造函数
我决定签字:public Point1 (Point1 other)
,但我不知道在写......
Point类的样子:
public class Point1
{
private int _x , _y;
public Point1 (Point1 other)
{
...
...
}
//other more constructors here...
}
我想:
public Point1 (Point1 other)
{
_x = other._x ;
_y = other._y;
}
但我几乎可以肯定我可以做得更好..
日Thnx
@Master C,我想你的意思是在你的例子中有'public Point1(Point1 other)',不是吗? – 2011-04-21 20:07:08
一个'Point'就是一个经常应该是不变的东西的例子,在这种情况下,实际上复制一个就不是必须的了。 – ColinD 2011-04-21 20:07:17
你为什么认为这应该是其他方式?所有复制构造函数应该做的就是复制其他对象的字段的值。 – uckelman 2011-04-21 20:08:20