내 질문은 기본적이지만 100 % 모든 것을 이해하고 싶습니다. 너무 많은 질문이 내 게시물 referes,하지만 난 만족스러운 답변을 찾지 못했습니다.열거 형 참조 또는 프리미티브 (예) - 얕은/깊은 복사
자바의 열거 형은 참조 유형이라는 것을 알고 있습니다. 시험의 복사 생성자가 얕은 복사본을 만들기 때문에
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);
}
출력은 매우 분명하다 :
10 100
200 3983
10 3983
을하지만 자바의 열거는 유형을 참조하고 있기 때문에 나는 한 가지를 이해하지의는 다음 코드를 살펴 보자.
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 <--- I thought that reference has been copied... not value
하지만이있어 :
10 ONE
200 THREE
10 ONE
질문 : 왜? 내 생각이 틀린 곳?