繁体   English   中英

有没有办法像Java中的C ++中那样以'&'形式获取变量作为参数?

[英]Is there any way to get the variable as the parameter as the '&' does in c++ in java?

我想创建一个可以更改给定参数值的void方法。 我试图在方法中对其进行修改,但由于它正在修改变量的副本,因此无法正常工作。

我知道我可以在c ++中做到这一点,如下所示:

void Foo::myMethod(MyObject& obj);

我知道我可以在方法中使用return语句,但是我想知道是否有一种方法可以在Java中执行上述操作,这就是我尝试过的方法:

public class LocalCallbackTest {

public static class MyClass {
    private MyClass oldClass = null;

    private MyClass newClass = null;

    private int value = 0;

    public MyClass() {

    }

    public MyClass(MyClass old) {
        this();
        oldClass = old;
    }

    /**
     * @return the newClass
     */
    public MyClass getNewClass() {
        return newClass;
    }

    /**
     * @return the oldClass
     */
    public MyClass getOldClass() {
        return oldClass;
    }

    /**
     * @return the value
     */
    public int getValue() {
        return value;
    }

    /**
     * @param newClass the newClass to set
     */
    public void setNewClass(MyClass newClass) {
        this.newClass = newClass;
    }

    /**
     * @param oldClass the oldClass to set
     */
    public void setOldClass(MyClass oldClass) {
        this.oldClass = oldClass;
    }

    /**
     * @param value the value to set
     */
    public void setValue(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();

        if (oldClass != null)
            sb.append("old class : \n\t").append(oldClass.getValue()).append("\n");
        if (newClass != null)
            sb.append("new class : \n\t").append(newClass.getValue()).append("\n");
        sb.append("actual class : \n\t").append(value).append("\n");

        return sb.toString();
    }

    private MyClass rollBack() {
        if (this.oldClass != null) {
            this.oldClass.setNewClass(this);

            return oldClass;
        }

        return this;
    }

    private MyClass nextMember() {
        if (this.newClass != null) {
            this.newClass.setOldClass(this);

            return newClass;
        }

        return this;
    }

    public static boolean rollBack(MyClass param) {
        if (param != (param = param.rollBack())) {
            return true;
        }
        return false;
    }

    public static boolean nextMember(MyClass param) {
        if (param != (param = param.nextMember())) {
            return true;
        }
        return false;
    }

    public static void test(int i) {
        i++;
    }
}

public static void main(String[] args) {

    MyClass test = new MyClass();

    test.setValue(0);

    for (int i = 1; i <= 5; i++) {
        MyClass test2 = new MyClass();

        test2.setValue(i);

        test2.setOldClass(test);
        test.setNewClass(test2);

        test = test.getNewClass();

        System.out.println(test.getValue());
    }

    int val=2;

    MyClass.test(val);

    System.out.println(val);

    }

}

谢谢您的帮助。

在C ++中,您可以通过复制或通过引用(使用&)传递参数,但是在Java中,您只能将对象作为引用传递,因此,作为参数传递的任何对象都将完全按照您的需要进行修改,而无需返回它。 (例如在C ++中使用&),如果它在您的示例中不起作用,则可能是由于某种逻辑。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM