[Java Basic] ch9_1.깊은복사와 얕은복사
🤔 얕은 복사
객체에 저장된 값을 복제
🤔 깊은 복사
객체가 참조하는 주소를 복제
public class Circle implements Cloneable{
private Point point;
private double radius;
public Circle(Point point, double radius) {
this.point = point;
this.radius = radius;
}
public Point getPoint() {
return point;
}
public double getRadius() {
return radius;
}
public void setPoint(Point point) {
this.point = point;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public Circle clone() {
try {
return (Circle) super.clone();
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
public Circle shallowCopy() {
return clone();
}
public Circle deepCopy() {
return new Circle(new Point(point.getX(), point.getY()), radius);
}
}
public class Point{
private int x;
private int y;
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
- 아래와 같이 하면 둘 다
false
입니다.
public class ShallowCopy {
public static void main(String[] args) {
Circle circle = new Circle(new Point(1, 1), 2.0);
Circle copy = circle.shallowCopy();
System.out.println(circle == copy);
}
}
public class DeepCopy {
public static void main(String[] args) {
Circle circle = new Circle(new Point(1, 1), 2.0);
Circle copy = circle.deepCopy();
System.out.println(circle == copy);
}
}
new Point(2, 2)
로 새 참조값을 넣어주면 얕은복사를 볼 수 없습니다.
public class ShallowCopy {
public static void main(String[] args) {
Circle circle = new Circle(new Point(1, 1), 2.0);
Circle copy = circle.shallowCopy();
circle.setRadius(3.0);
circle.setPoint(new Point(2, 2));
System.out.println(circle.getRadius());
System.out.println(circle.getPoint().getX()+", "+circle.getPoint().getY());
System.out.println(copy.getRadius());
System.out.println(copy.getPoint().getX()+", "+copy.getPoint().getY());
}
}
public class DeepCopy {
public static void main(String[] args) {
Circle circle = new Circle(new Point(1, 1), 2.0);
Circle copy = circle.deepCopy();
circle.setRadius(3.0);
circle.setPoint(new Point(2, 2));
System.out.println(circle.getRadius());
System.out.println(circle.getPoint().getX()+", "+circle.getPoint().getY());
System.out.println(copy.getRadius());
System.out.println(copy.getPoint().getX()+", "+copy.getPoint().getY());
}
}
- radius는 double(primitive type)이므로 값이 복사 되었습니다.
public class ShallowCopy {
public static void main(String[] args) {
Circle circle = new Circle(new Point(1, 1), 2.0);
Circle copy = circle.shallowCopy();
circle.setRadius(3.0);
circle.getPoint().setX(2);
circle.getPoint().setY(2);
System.out.println(circle.getRadius());
System.out.println(circle.getPoint().getX()+", "+circle.getPoint().getY());
System.out.println(copy.getRadius());
System.out.println(copy.getPoint().getX()+", "+copy.getPoint().getY());
}
}
-----
3.0
2, 2
2.0
2, 2
-----
public class DeepCopy {
public static void main(String[] args) {
Circle circle = new Circle(new Point(1, 1), 2.0);
Circle copy = circle.deepCopy();
circle.setRadius(3.0);
circle.getPoint().setX(2);
circle.getPoint().setY(2);
System.out.println(circle.getRadius());
System.out.println(circle.getPoint().getX()+", "+circle.getPoint().getY());
System.out.println(copy.getRadius());
System.out.println(copy.getPoint().getX()+", "+copy.getPoint().getY());
}
}
-----
3.0
2, 2
2.0
1, 1
-----
댓글남기기