通过java中的引用传递枚举 [英] passing enums by ref in java

查看:300
本文介绍了通过java中的引用传递枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过java引用枚举参数?任何解决方案?

解决方案

在Java中,您不能通过引用传递任何参数。



  public class EnumReference {
public YourEnumType ref;
}

然后你会这样使用:

  public void someMethod(EnumReference reference){
reference.ref = YourEnumType.Something;
}

现在,引用将包含一个不同的枚举值;基本上模仿传递参考。


How can i pass enum parameter by reference in java? Any solution?

解决方案

In Java you cannot pass any parameters by reference.

The only workaround I can think of would be to create a wrapper class, and wrap an enum.

public class EnumReference {
    public YourEnumType ref;
}

And then you would use it like so:

public void someMethod(EnumReference reference) {
    reference.ref = YourEnumType.Something;
}

Now, reference will contain a different value for your enum; essentially mimicking pass-by-reference.

这篇关于通过java中的引用传递枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆