메뉴 바로가기 검색 및 카테고리 바로가기

한빛미디어

뇌를 자극하는 java 프로그래밍

뇌를 자극하는 java 프로그래밍 질의응답 게시판입니다.

object클래스의 clone() 메서드 부분에서의 질문입니다.

2008-05-01

|

by 자바

1950

package a;

class multi {
        public static void main(String args[]) {
                Point obj1 = new Point(10,10);
                Point obj2 = (Point)obj1.clone();
                obj2.setX(50);
                obj2.setY(100);
                System.out.println("obj1 = " + obj1);
                System.out.println("obj2 = " + obj2);
        }
}

class Point implements Cloneable {
        int x,y;
        Point(int x, int y) {
                this.x = x;
                this.y = y;
        }
        void setX(int x) {
                this.x = x;
        }
        void setY(int y) {
                this.y = y;
        }
        public Object clone() {
                try {
                        return super.clone();            <-  왜 super클래스의 clone메서드를
                                                                    호출해야 하는지 의미를 잘 모르겠
                                                                    습니다.
                }
                catch(CloneNotSupportedException e) {
                        return null;
                }
        }
        public String toString() {
                return "(" + x + "," + y + ")";
        }
}
댓글 입력
자료실