Equals Clone 오버라이딩 연습문제

gustjtmd·2022년 2월 2일
0

Java 연습문제

목록 보기
4/4
아래의 Point 클래스와 Rectangle 클래스에 내용 비교를 위한 equals 메소드를 각각
삽입하자. 그리고 정의한 equals 메소드의 확인을 위한 main 메소드도 직접 정의하자

class Point{
	private int xPos;
    private int yPos;
    public Point(int x, int y){
    	xPos = x;
        yPos = y;
    }
}
class Rectangle{
	private Point upperLeft;	//좌측 상단 좌표
    private Point lowerRight;	//우측 하단 좌표
    public Rectangle(int x1, int y1, int x2, int y2){
    	upeerLeft = new Point(x1, y1);
        lowerRIght = new Point(x2, y2);
    }
}

---------------------------------------------------------------------------

class Point{
    private int xPos;
    private int yPos;
    public Point(int x, int y){
        xPos = x;
        yPos = y;
    }
    public boolean equals(Object obj){
        Point p = (Point)obj;
        if(xPos == p.xPos && yPos == p.yPos)
            return true;
        else
            return false;
    }
}
class Rectangle{
    private Point upperLeft;    // 좌측 상단 좌표
    private Point lowerRight;   // 우측 하단 좌표
    public Rectangle(int x1, int y1, int x2, int y2){
        upperLeft = new Point(x1, y1);
        lowerRight = new Point(x2, y2);
    }
    public boolean equals(Object obj){

        Rectangle r = (Rectangle)obj;
        if(upperLeft.equals(r.upperLeft) && lowerRight.equals(r.lowerRight))
            return true;
        else
            return false;
    }
}
public class EqualsTest {
    public static void main(String[] args) {
        Point point1 = new Point(3, 4);
        Point point2 = new Point(3, 4);

        if(point1.equals(point2))
            System.out.println("좌표가 같습니다.");
        else
            System.out.println("좌표가 다릅니다");

        Rectangle rectangle1 = new Rectangle(1, 2, 3, 4);
        Rectangle rectangle2 = new Rectangle(1, 2, 3, 4);

        if(rectangle1.equals(rectangle2))
            System.out.println("좌표가 같습니다.");
        else
            System.out.println("좌표가 다릅니다.");
    }
}


좌표가 같습니다.
좌표가 같습니다.
깊은 복사를 위한 clone의 오버라이딩

다음 클래스 정의에서 PersonalInfo의 clone 메소드 호출시 깊은 복사가 이뤄지도록
clone메소드를 오버라이딩하자.

class Business implements Cloneable {
    private String company;
    private String work;
    
    public Business(String company, String work) {
        this.company = company;
        this.work = work;
    }
    
    public void showBusinessInfo() {
        System.out.println("회사: " + company);
        System.out.println("업무: " + work);
    }
}

class PersonalInfo implements Cloneable {
    private String name;
    private int age;
    private Business bz;
    
    public PersonalInfo(String name, int age, String company, String work) {
        this.name = name;
        this.age = age;
        bz = new Business(company, work);
    }	
    
    public void showPersonalInfo() {
        System.out.println("이름: " + name);
        System.out.println("나이: " + age);
        bz.showBusinessInfo();
    }
  
}


-----------------------------------------------------------------------------


class Business implements Cloneable {
    private String company;
    private String work;
    
    public Business(String company, String work) {
        this.company = company;
        this.work = work;
    }
    
    public void showBusinessInfo() {
        System.out.println("회사: " + company);
        System.out.println("업무: " + work);
    }
    
    public Business clone() throws CloneNotSupportedException {
        Business copy = (Business)super.clone();
        return copy;
    }
}

class PersonalInfo implements Cloneable {
    private String name;
    private int age;
    private Business bz;
    
    public PersonalInfo(String name, int age, String company, String work) {
        this.name = name;
        this.age = age;
        bz = new Business(company, work);
    }	
    
    public void showPersonalInfo() {
        System.out.println("이름: " + name);
        System.out.println("나이: " + age);
        bz.showBusinessInfo();
    }
    
    public PersonalInfo clone() throws CloneNotSupportedException {
        PersonalInfo cpy = (PersonalInfo)super.clone();
        cpy.bz = bz.clone();
        return cpy;
    }
}

class DeepCopyTest {
    public static void main(String[] args) {
        try {
            PersonalInfo org =
                      	new PersonalInfo("James", 22, "SimpeSound", "encoding");

            PersonalInfo cpy = org.clone();
			
	    org.showPersonalInfo();
            cpy.showPersonalInfo();
        }
        catch(CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}

이름 : James
나이 : 22
회사 :SimpeSound
업무 : encoding
이름 : James
나이 : 22
회사 :SimpeSound
업무 : encoding
profile
반갑습니다

0개의 댓글