java枚举 或_java-枚举类型引用或基元(带有示例)-浅/深复制

我的问题很基本,但是我想100%理解所有内容. SO中的许多问题都与我的帖子有关,但我找不到令人满意的答案.

我们知道Java中的Enums是引用类型.让我们考虑以下代码段:

public static class A {

public int i;

public A(int i) {

this.i = i;

}

}

public static class Test {

int i;

A a;

public Test(int i, A a){

this.i = i;

this.a = a;

}

public Test(Test oldTest){

this.i = oldTest.i;

this.a = oldTest.a;

}

}

public static void main(String[] args) {

Test t1 = new Test(10, new A(100));

System.out.println(t1.i + " " + t1.a.i);

Test t2 = new Test(t1);

t2.i = 200;

t2.a.i = 3983;

System.out.println(t2.i + " " + t2.a.i);

System.out.println(t1.i + " " + t1.a.i);

}

输出非常明显,因为Test的副本构造函数进行了浅表复制:

10 100

200 3983

10 3983

但是,因为Java中的枚举也是引用类型,我不了解一件事.让我们用Enum代替A类:

public static enum TestEnum {

ONE, TWO, THREE;

}

public static class Test {

int i;

TestEnum enumValue;

public Test(int i, TestEnum enumVar){

this.i = i;

this.enumValue = enumVar;

}

public Test(Test oldTest){

this.i = oldTest.i;

this.enumValue = oldTest.enumValue; // WHY IT IS OK ??

}

}

public static void main(String[] args) {

Test t1 = new Test(10, TestEnum.ONE);

System.out.println(t1.i + " " + t1.enumValue);

Test t2 = new Test(t1);

t2.i = 200;

t2.enumValue = TestEnum.THREE; // why t1.emunValue != t2.enumValue ??

System.out.println(t2.i + " " + t2.enumValue);

System.out.println(t1.i + " " + t1.enumValue);

}

我期望输出:

10 ONE

200 THREE

10 THREE

但是我有:

10 ONE

200 THREE

10 ONE

问题:为什么?我的想法哪里不对?

解决方法:

这里的枚举没有什么特别的.如果使用字符串或任何类型,基本上您将看到完全相同的行为.

您的两个测试对象是完全分开的.当你写:

t2.enumValue = TestEnum.THREE;

您将第二个对象中的enumValue字段的值更改为对TestEnum.THREE引用的对象的引用.

两个enumValue字段(一个通过t1和一个通过t2)完全分开.更改一个字段不会更改另一个字段.

现在,如果改为使您的枚举可变(我强烈劝阻),并将您的代码更改为如下所示:

t2.enumValue.someMutableField = "a different value";

…这将通过t1.enumValue可见,因为它们都引用相同的对象.

在更改Test实例中的字段和更改通过Test实例碰巧到达的对象中的字段之间进行区分非常重要.

同样,这实际上与枚举无关.您可能会发现,通过将enumValue字段更改为String字段并进行这种尝试,可以更轻松地解决这个问题.

标签:shallow-copy,enums,deep-copy,java

来源: https://codeday.me/bug/20191030/1970770.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值