如何修改传入Java方法的枚举

发布于 2024-10-18 06:35:15 字数 484 浏览 8 评论 0原文

public class Test {
    private Result result;

    public Test(Result res){
        this.result = res;
    }

    public void alter(){
        this.result = Result.FAIL;
    }
}

public enum Result{ PASS, FAIL, MORE};
public Result myResult = Result.PASS;

Test test = new Test(myResult);
test.alter();

在上面的示例中,如何修改 alter 方法内的变量 myResult ?由于 Java 是按值传递的,因此该示例只是将其值分配给 this.result

public class Test {
    private Result result;

    public Test(Result res){
        this.result = res;
    }

    public void alter(){
        this.result = Result.FAIL;
    }
}

public enum Result{ PASS, FAIL, MORE};
public Result myResult = Result.PASS;

Test test = new Test(myResult);
test.alter();

In the above example, how would I modify the variable myResult inside the alter method? Since Java is pass by value, the example simply assigns its value to this.result.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

摇划花蜜的午后 2024-10-25 06:35:15

基本上,你不能,因为 Java 是按值传递的。

在 Java 中,最接近引用传递行为的是创建一个带有 getter 和 setter 的“holder”类;例如

public class ResultHolder {
    private Result value;
    public ResultHolder(Result initial) { value = initial; }
    public void setValue(Result newValue) { value = newValue; }
    public Result getValue() { return value; }
}

,您可以将alter() 编写为:

public void alter(ResultHolder holder, Result newValue) {
    holder.setValue(newValue);
}

请注意,这不是真正的按引用传递。

Basically, you can't, because Java is pass-by-value.

The closest you can get to pass-by-reference behavior in Java is to create a "holder" class with a getter and setter; e.g.

public class ResultHolder {
    private Result value;
    public ResultHolder(Result initial) { value = initial; }
    public void setValue(Result newValue) { value = newValue; }
    public Result getValue() { return value; }
}

Then, you could write alter() as:

public void alter(ResultHolder holder, Result newValue) {
    holder.setValue(newValue);
}

Note that this is not real pass-by-reference.

会傲 2024-10-25 06:35:15

您无法修改实际的枚举值。它们本质上是被命名为常量的类。
如果您想要更改枚举实例内的行为,那么您不需要枚举(如果您可以更改对象,那么该对象的其他使用者就不能将其视为常量)。

You can't modify the actual enum values. They're essentially classes that are named constants.
If you want altered behavior inside an enum instance then you don't want an enum ( if you can alter the object then other consumers of the object can't treat it as a constant).

场罚期间 2024-10-25 06:35:15

简单地说,用 Java 是做不到的。

Simply put, you can't do it in Java.

抱猫软卧 2024-10-25 06:35:15

由于 myResult 是类字段,因此您可以在需要时通过分配其他值来更改它:
myResult = Result.MORE;。您在哪里编写此代码并不重要。

Since myResult is the class field you can change it when you wish by assigning other value:
myResult = Result.MORE;. it does not matter where do you write this code.

人│生佛魔见 2024-10-25 06:35:15
public class Test {
    private Result result;

    public Test(Result res){
        this.result = res;
    }

    public Result alter(){          // signature changed
        this.result = Result.FAIL;
        return this.result;         // new
    }
}

_

public enum Result{ PASS, FAIL, MORE};
public Result myResult = Result.PASS;

Test test = new Test(myResult);
myResult = test.alter();            // change the value
public class Test {
    private Result result;

    public Test(Result res){
        this.result = res;
    }

    public Result alter(){          // signature changed
        this.result = Result.FAIL;
        return this.result;         // new
    }
}

_

public enum Result{ PASS, FAIL, MORE};
public Result myResult = Result.PASS;

Test test = new Test(myResult);
myResult = test.alter();            // change the value
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文