在Java中,您如何返回对象的引用,因此可以使用分配操作员修改对象



我理解java通过值(包括参考)通过了所有内容。由于调用函数具有正式参数对象的副本 - 参考文献 - 它无法使用默认分配操作员(=)修改原始对象。我假设引用等同于C中的指针 - 它保留了实际对象变量的地址。

编辑:即使其他问题也提出了类似的问题,我仍在尝试查看是否可以使用分配运算符来使交换操作员超载,以使交换运算符(Java)不支持用户定义的运算符过载。另外,我希望有人能提供一种新的方法。

这是我要测试这个概念的类,看看我是否可以在对象声明的范围之外获取实际对象。

public class Time {
    public int currentTime = 0;
    public static int iD =0 ;
    public final int uniqueObjId = iD;
    public Time(int settingTime) {
        currentTime = settingTime;
        iD++;
    }
    public int getTime() {
        return currentTime;
    }
    public static long getCurrTime() {
        return System.currentTimeMillis();
    }
    public void setTime(int currentTime) {
        this.currentTime = currentTime;
    }
    public static void main(String[] args) {
        Time t1 = new Time(100);
        Time t2 = new Time(200);
        System.out.println("Before testInt is " + t1.getTime());
        System.out.println("Before testInt2 is " + t2.getTime());
        System.out.println("testInt Objectid is " + t1.uniqueObjId);
        System.out.println("testInt2 Objectid is " + t2.uniqueObjId);
        swap(t1, t2);
        System.out.println("testInt is " + t1.getTime());
        System.out.println("testInt2 is " + t2.getTime());

    }
    public static void swap(Time x, Time y){
        Time temp = new Time(300);
        System.out.println("Before x is " + x.uniqueObjId);
        System.out.println("Before y is " + y.uniqueObjId);
        System.out.println("Before temp is " + temp.uniqueObjId);
        x = x.getObj();
        y = y.getObj();
        temp = x;
        x = y;
        y = temp;
        System.out.println("After x objectId is " + x.uniqueObjId);
        System.out.println("After y objectId is " + y.uniqueObjId);
        System.out.println("After temp objectId is " + temp.uniqueObjId);
    }
    public Time getObj(){
    return this;
    }
}

这是结果:

Before testInt is 100
Before testInt2 is 200
testInt Objectid is 0
testInt2 Objectid is 1
Before x is 0
Before y is 1
Before temp is 2
After x objectId is 1
After y objectId is 0
After temp objectId is 0
testInt is 100
testInt2 is 200

根据结果,x和y指向MAIM()中声明的实际对象T1和T2,但是,将T1分配给temp and T2和T2到T1,最后临时到T2不复制其成员,主要是当前时间变量。因此,我假设一种方法来获取对象的所有成员在对象范围之外复制的所有成员的方法是超载默认分配运算符?请让我知道分配运算符是否会执行实际分配,而不仅仅是将当前变量指向另一个变量的引用对象。

您需要返回x和y的那些值,然后分配给T1和T2。在main()中: -

List<Time> list = swap(t1,t2);
t1 = list.get(0); t2 = list.get(1);

和更改swap()如下:

public static List<Time> swap(Time x, Time y){
    Time temp = new Time(300);
    System.out.println("Before x is " + x.uniqueObjId);
    System.out.println("Before y is " + y.uniqueObjId);
    System.out.println("Before temp is " + temp.uniqueObjId);
    x = x.getObj();
    y = y.getObj();
    temp = x;
    x = y;
    y = temp;
    List<Time> list = new ArrayList<Time>();
    list.add(x);
    list.add(y);
    System.out.println("After x objectId is " + x.uniqueObjId);
    System.out.println("After y objectId is " + y.uniqueObjId);
    System.out.println("After temp objectId is " + temp.uniqueObjId);
    return(list);
}

相关内容

最新更新