Java 创建并返回同一对象的副本

Java 创建并返回同一对象的副本,java,copy,this,clone,Java,Copy,This,Clone,在这个类中,Coord方法public-Coord-copy()应该创建并返回一个新的Coord值,其行/列表示形式与当前对象相同。为什么当我进行this=clone时它会给我错误 public class Coord { public final int r; public final int c; public Coord(int r, int c) { this.r = r; this.c = c; }

在这个类中,
Coord
方法
public-Coord-copy()
应该创建并返回一个新的Coord值,其行/列表示形式与当前对象相同。为什么当我进行
this=clone
时它会给我错误

public class Coord {

    public final int r;
    public final int c;


    public Coord(int r, int c)
    {
        this.r = r;
        this.c = c;
    }

    public Coord step(Direction d)
    {
        if(d == Direction.N)
        {
            Coord newValue = new Coord(r + 1, c);
            return newValue;
        }
        else if(d == Direction.S)
        {
            Coord newValue = new Coord(r - 1, c);
            return newValue;
        }
        else if(d == Direction.E)
        {
            Coord newValue = new Coord(r, c + 1);
            return newValue;
        }
        else if(d == Direction.W)
        {
            Coord newValue = new Coord(r, c - 1);
            return newValue;
        }

        else
            return this;

    }

    public Coord copy()
    {
        Coord clone = new Coord(r, c);
        this = clone;
        return clone;

    }


}

我想你应该这样做:

public Coord copy() {
    Coord clone = new Coord(r, c);
    return clone;
}
在这种情况下,它甚至可以缩短为:

public Coord copy() {
    return new Coord(r, c);
}
是对当前对象的引用(在本例中,是当前的
Coord
对象),Java不允许您将新值分配给
this
。如果您想要复制,那么您只需要创建新对象并返回该新对象


也许这样想会有帮助

您可能正在使用
Coord
copy()
方法进行复制:

int x = ...; //x is some int
int y = ...; //y is some int

Coord coordOriginal = new Coord(x, y);
//coordOriginal points to a new Coord object. Let's call it "A".

Coord coordCopy = coordOriginal.copy();
//coordCopy points to to a new Coord object.  Let's call it "B".
//coordOriginal still points to Coord object "A".
如果
copy()
工作正常,您可能希望
coordOriginal
coordCopy
指向两个不同的对象
coordOriginal
指向
Coord
对象A,
coordCopy
指向
Coord
对象B

现在,让我们假设您可以像示例代码的
copy()
方法一样,为
这个
分配一个新值:

//in this case, "this" points to object "A".
public Coord copy()
{
    //creates an object "B"
    Coord clone = new Coord(r, c);

    //***overwrites object "A" with object "B"!***
    this = clone;

    //returns object "B"
    return clone;
}
//object "A" gets garbage collected?
因此,您可以看到,如果您可以在
copy()
方法中为
这个
分配一个新对象(Java不允许您),那么您将更改原始
Coord
对象,而您只想做一个副本


如问题注释中所述,另一种方法是复制构造函数:

public class Coord {

    public final int r;
    public final int c;

    public Coord(int r, int c) {
        this.r = r;
        this.c = c;
    }

    //copy constructor
    public Coord(Coord other) {
        this(other.r, other.c);
    }

}
然后你可以像这样复制:

int x = ...; //x is some int
int y = ...; //y is some int

Coord coordOriginal = new Coord(x, y);
Coord coordCopy = new Coord(coordOriginal);

首先,
这个
是一个关键字而不是一个变量,所以分配给关键字没有意义

在第二种情况下,您不能将
分配给this
,因为在Java中,这是禁止的,
this
指的是您正在调用当前方法的对象,尽管在技术上可能,但这是不允许的


要获得相同的内容,必须从方法外部或通过访问容器将对象存储的位置重新指定给新创建的
Coord
。另一种方法是直接修改对象本身的成员变量,但它不是同一件事。

您所说的“要获得相同的东西,必须从方法外部或通过访问容器将对象存储到新创建的坐标”是什么意思,这听起来对你来说是个不错的模式。