☕ Java

[Java] Day10 - 상속 심화 (오버라이딩)

harveydent 2023. 5. 16. 19:32
728x90

오버라이딩

오버라이딩이란 상속 관계에 있는 부모 클래스에서 이미 정의된 메서드를 자식 클래스에서 같은 시그니처를 갖는 메서드로 다시 정의하는 것을 의미합니다. 오버라이딩을 통해 기존에 있던 메서들를 원하는 기능으로 바꾸어 쓸 수 있습니다.

실습 1

프로그램 개요

점 클래스 Point가 있습니다.

int x, int y를 멤버 변수로 가지고 있습니다.

x, y의 좌표가 같다면 같은 점으로 인식할 수 있도록 해주세요!~~

main() {

    Point[] data = new Point[3];

    data[0]에는 점(10, 20)

    data[1]에는 점(123, 20)

    data[2]에는 점(10, 20)

        이 저장되어 있습니다.

    data[0]과 data[1],

    data[0]과 data[2]를 비교해 주세요!~~

}

 

색깔점 클래스 ColorPoint가 있습니다.

int x, int y, String color를 멤버 변수로 가지고 있습니다.

x, y의 좌표가 같아도 색이 다르면 다른 점으로 인식할 수 있도록

main() {

    ColorPoint[] data = new ColorPoint[3];

    data[0]에는 파랑(10, 20)

    data[1]에는 파랑(10, 20)

    data[2]에는 빨강(10, 20)

        이 저장되어 있습니다.

    data[0]과 data[1],

    data[0]과 data[2]를 비교해 주세요!~~

}

 

코드 구현

package class03;

class Point {
	int x;
	int y;
	
	Point(int x, int y) {
		this.x = x;
		this.y = y;
	}
	
	void printInfo() {
		System.out.println("(" + this.x + ", " + this.y + ")");
	}

	@Override
	public boolean equals(Object obj) {
		Point point = (Point)obj;
		if (this.x == point.x && this.y == point.y) {
			return true;
		}
		return false;
	}
}

class ColorPoint extends Point {
	String color;
	
	ColorPoint(int x, int y, String color) {
		super(x, y);
		this.color = color;
	}
	
	void printInfo() {
		System.out.println("(" + this.x + ", " + this.y + ", " + this.color + ")");
	}

	@Override
	public boolean equals(Object obj) {
		ColorPoint colorpoint = (ColorPoint)obj;
		if (this.x == colorpoint.x && this.y == colorpoint.y && this.color.equals(colorpoint.color)) {
			return true;
		}
		return false;
	}
}

public class Test01 {

	public static void main(String[] args) {
		
		Point[] data = new Point[3];
		data[0] = new Point(10, 30);
		data[1] = new Point(123, 30);
		data[2] = new Point(10, 30);
		ColorPoint[] data2 = new ColorPoint[3];
		data2[0] = new ColorPoint(10, 20, "파랑");
		data2[1] = new ColorPoint(10, 20, "파랑");
		data2[2] = new ColorPoint(10, 20, "빨강");
		
		for(int i = 1; i <= 2; i++) {
			if(data[0].equals(data[i])) {
				System.out.println("같아~");
			} else {
				System.out.println("달라~");
			}
		}
		
		for(int i = 1; i <= 2; i++) {
			if(data2[0].equals(data2[i])) {
				System.out.println("같아~");
			} else {
				System.out.println("달라~");
			}
		}

	}

}

 

실행 결과

실습 2

프로그램 개요

===== 메뉴 =====
1. 점 생성
2. 점 목록 출력
3. 점 1개 출력
4. 점 이동
5. 색 변경
6. 점 합치기 (+)
7. 프로그램 종료
===============

 

Point[] data=new Point[3]; // 최대 3개

    new Point(); // 점(0, 0)
    new Point(3, 4); // 점(3, 4)
    new ColorPoint(); // 검정(0, 0)
    new ColorPoint(1, 2); // 검정(1, 2)
    new ColorPoint("빨강", 3, 4); // 빨강(3, 4)

점(1,2)
검정(2,3)
분홍(-1,-3)


point.move(); -> +1 +1
point.move(10); -> +10 +10
point.move(1,2); -> +1 +2
point.changeColor("빨강")

1,2 + 2,3 => 3,5

 

코드 구현

package class04;

import java.util.Scanner;

class Point {
	String color;
	int x;
	int y;

	Point() {
		this(0, 0);
	}
	
	Point(int x, int y) {
		this.color = "점";
		this.x = x;
		this.y = y;
	}
	
	void move() {
		move(1, 1);
	}
	
	void move(int x) {
		move(x, x);
	}
	
	void move(int x, int y) {
		this.x += x;
		this.y += y;
	}
	
	void printInfo() {
		System.out.println(this.color + "(" + this.x + ", " + this.y + ")");
	}

	@Override
	public String toString() {
		
		return "점(" + this.x + ", " + this.y + ")";
	}
	
	
	
}

class ColorPoint extends Point {
	
	ColorPoint() {
		this("검정", 0, 0);
	}
	
	ColorPoint(int x, int y) {
		this("검정", x, y);
	}
	
	ColorPoint(String color, int x, int y) {
		super(x, y);
		this.color = color;
	}
	
	void changeColor(String color) {
		this.color = color;
	}

	@Override
	public String toString() {
	
		return this.color + "(" + this.x + ", " + this.y + ")";
	}
}

public class Test01 {

	// INPUT : 점 2개를 받아서
	// OUTPUT : 출력하고 끝! void
	public static void addPoint(Point point1, Point point2) {
		Point resPoint = new Point();
		resPoint.x = point1.x + point2.x;
		resPoint.y = point1.y + point2.y;
		
		System.out.println(point1);
		System.out.println("더하기");
		System.out.println(point2);
		System.out.println("결과) " + resPoint);
	}
	
	public static void main(String[] args) {
	
		Scanner sc = new Scanner(System.in);
		
		String menu = "===== 메뉴 =====\n"
					+ "1. 점 생성\n"
					+ "2. 점 목록 출력\n"
					+ "3. 점 1개 출력\n"
					+ "4. 점 이동\n"
					+ "5. 색 변경\n"
					+ "6. 점 합치기 (+)\n"
					+ "7. 프로그램 종료\n"
					+ "===============";
		final int N = 3;
		
		Point[] data = new Point[N];
		int dataCnt = 0;
		
		int action;
		int point;
		int x = 0;
		int y = 0;
		String color;
		boolean flag = false;
		boolean flagPoint = false;
		
		while (true) {
			System.out.println(menu);
			action = sc.nextInt();
			
			if (action == 7) {
				break;
			}
			else if (action == 1) {
				if (dataCnt >= N) {
					System.out.println("생성할 공간이 없습니다!");
					continue;
				}
				while (true) {
					System.out.println("색깔을 입력하시겠습니까?");
					System.out.println("yes) 1 no) 2");
					action = sc.nextInt();
					if (action == 2) {
						color = null;
						break;
					} else if (action == 1) {
						while (true) {
							System.out.println("색깔을 입력해주세요. ※입력하지 않으면 검정");
							sc.nextLine();
							color = sc.nextLine();
							System.out.println("'" + color + "' 이(가) 맞습니까?");
							while (true) {
								System.out.println("yes) 1 no) 2");
								action = sc.nextInt();
								if (action == 2) {
									System.out.println("색깔을 다시 입력해주세요\n");												
								} else if (action == 1) {
									flag = true;
								} else {
									System.out.println("잘못 입력하셨습니다.");
									continue;
								}
								break;
							}
							if (flag) {
								break;
							}
						}
					} else {
						System.out.println("잘못 입력하셨습니다.");
						continue;
					}
					break;
				}
				flag = false;
				
				while (true) {
					System.out.println("점을 입력하시겠습니까?");
					System.out.println("yes) 1 no) 2");
					action = sc.nextInt();
					if (action == 2) {
						flagPoint = true;
						break;
					} else if (action == 1) {
						while (true) {
							System.out.println("x 좌표를 입력해주세요.");
							x = sc.nextInt();
							System.out.println("'" + x + "' 이(가) 맞습니까?");
							while (true) {
								System.out.println("yes) 1 no) 2");
								action = sc.nextInt();
								if (action == 2) {
									System.out.println("x 좌표를 다시 입력해주세요\n");													
								} else if (action == 1) {
									flag = true;
								} else {
									System.out.println("잘못 입력하셨습니다.");
									continue;
								}
								break;
							}
							if (flag) {
								break;
							}
						}
						flag = false;
						
						while (true) {
							System.out.println("y 좌표를 입력해주세요.");
							y = sc.nextInt();
							System.out.println("'" + y + "' 이(가) 맞습니까?");
							while (true) {
								System.out.println("yes) 1 no) 2");
								action = sc.nextInt();
								if (action == 2) {
									System.out.println("y 좌표를 다시 입력해주세요\n");													
								} else if (action == 1) {
									flag = true;
								} else {
									System.out.println("잘못 입력하셨습니다.");
									continue;
								}
								break;
							}
							if (flag) {
								break;
							}
						}
						flag = false;
					} else {
						System.out.println("잘못 입력하셨습니다.");
						continue;
					}
					break;
				}
				
				if (flagPoint && color == null) {
					data[dataCnt++] = new Point();
				} else if (!flagPoint && color == null) {
					data[dataCnt++] = new Point(x, y);
				} else if (flagPoint && "".equals(color)) {
					data[dataCnt++] = new ColorPoint();
				} else if (!flagPoint && "".equals(color)) {
					data[dataCnt++] = new ColorPoint(x, y);
				} else {
					data[dataCnt++] = new ColorPoint(color, x, y);
				}
				
			} else if (action == 2) {
				if (dataCnt == 0) {
					System.out.println("생성된 점이 없습니다.");
					continue;
				}
				for (int i = 0; i < dataCnt; i++) {
					System.out.print(i + 1 + ") ");
					data[i].printInfo();
				}
			} else if (action == 3) {
				if (dataCnt == 0) {
					System.out.println("생성된 점이 없습니다.");
					continue;
				}
				while (true) {
					System.out.println("출력할 점 번호를 입력해주세요");
					action = sc.nextInt();
					if (action < 1 || action > dataCnt) {
						System.out.println("존재하지 않는 점 입니다. 다시 입력해주세요.");
						continue;
					}
					System.out.print(action + 1 + ") ");
					data[action - 1].printInfo();
				}
			} else if (action == 4) {
				while (true) {
					System.out.println("이동할 점을 입력해주세요.");
					point = sc.nextInt();
					if (point < 1 || point > dataCnt) {
						System.out.println("존재하지 않는 점 입니다. 다시 입력해주세요.");
						continue;
					}
					System.out.println("1칸씩이동) 1");
					System.out.println("1번이동) 2");
					System.out.println("x, y이동) 3");
					action = sc.nextInt();
					
					if (action == 3) {
						while (true) {
							System.out.println("x좌표를 이동할 값을 입력해주세요.");
							x = sc.nextInt();
							System.out.println("y좌표를 이동할 값을 입력해주세요.");
							y = sc.nextInt();
							System.out.println(x + " " + y + "가 맞습니까?");
							while (true) {
								action = sc.nextInt();
								System.out.println("yes) 1 no) 2");
								if (action == 2) {
									System.out.println("다시 입력해주세요\n");													
								} else if (action == 1) {
									flag = true;
								} else {
									System.out.println("잘못 입력하셨습니다.");
									continue;
								}
								break;
							}
							if (flag) {
								break;
							}
						}
						flag = false;
						
						data[point - 1].move(x, y);
						
					} else if (action == 2) {
						System.out.println("이동할 값을 입력해주세요.");
						x = sc.nextInt();
						data[point - 1].move(x);
					} else if (action == 1) {
						data[point - 1].move();
					} else {
						System.out.println("잘못 입력하셨습니다.");
						continue;
					}
					System.out.println(point + "번 점이 이동되었습니다.");
					break;
				}
			} else if (action == 5) {
				while (true) {
					System.out.println("색을 변경할 점을 입력해주세요.");
					point = sc.nextInt();
					if (point < 1 || point > dataCnt) {
						System.out.println("존재하지 않는 점 입니다. 다시 입력해주세요.");
						continue;
					}
					if (data[point - 1] instanceof ColorPoint) {
						System.out.println("색을 변경할 수 없는 점 입니다.");
						break;
					}
					System.out.println("변경할 색을 입력해주세요.");
					color = sc.next();
					System.out.println("'" + color + "' 이(가) 맞습니까?");
					while (true) {
						System.out.println("yes) 1 no) 2");
						action = sc.nextInt();
						if (action == 2) {
							System.out.println("다시 입력해주세요\n");													
						} else if (action == 1) {
							flag = true;
						} else {
							System.out.println("잘못 입력하셨습니다.");
							continue;
						}
						break;
					}
					ColorPoint colorpoint = (ColorPoint)data[point - 1];
					colorpoint.changeColor(color);
					System.out.println(point + "번 색이 " + color +"로 변경되었습니다.");
					if (flag) {
						break;
					}
				}
				flag = false;
			} else if (action == 6) {
				
			} else {
				System.out.println("잘못 입력하셨습니다.");
			}
		}
		
	}
}

GitHub

https://github.com/Qkrwnsgus0522/Java

 

GitHub - Qkrwnsgus0522/Java

Contribute to Qkrwnsgus0522/Java development by creating an account on GitHub.

github.com

 

728x90