Java(자바) 13日次

블로그 移轉·2022년 11월 9일

Java

목록 보기
12/20

Java 面接 族譜 整理 (13日次)

1. 메소드 오버라이드(Override)란?

Polymorphism(多形性)의 核心中 하나。
메소드 오버라이드는 相續關係에서 父母와 子息의 函數의 return타입、函數名、파라미터가 모두 同一하고 具現內容만 다르게 하는 것을 意味한다。

Polymorphism、오버라이드된 函數를 呼出하면 父母函數의 住所를 子息函數의 住所로 變更되어 子息의 函數가 呼出된다。

實際로는 住所가 變更되는거지만 一旦 只今은 덮어쓴다고 생각해도 된다。(父母것을 呼出해도 自息것)

變數는 오버라이드 對象이 아니다。

오버라이드 條件 → 函數 宣言部 一致
相續關係에서 父母와 子息의 函數의 return타입、函數名、파라미터가 全部 同一하고 具現內容만 다르게 한다。

오버라이드 메소드 呼出
→ 오버라이드된 函數를 呼出하면 父母函數의 住所를 子息函數의 住所로 變更해서 子息의 函數가 呼出된다。(= 子息것)

2. 오버로드와 오버라이드의 差異는?

오버로드 vs 오버라이드

  • 오버로드(Overload)
    → 이름만 同一한 他 函數(파라미터타입、個數가 다름)를 새롭게 生成
  • 오버라이드(Override)
    → 相續(繼承)關係에서 父母函數와 同一한 函數의 具現內容만 다르게 修正 (덮어쓴다)

3. @Override에 對하여 說明하시오。

Annotation(註釋) → @Override

오버라이드하라고 좀 더 明確하게 開發者들에게 알려주는 것。
컴파일時 誤謬 等을 發生시켜 警告나 確認을 하는 目的으로 使用한다。
→ 安全性을 爲한 것

@Override는 現在 메소드가 父母클래스의 메소드를 오버라이드한 것임을 컴파일러에게 明視하고、父母클래스에 該當하는 메소드가 없다면 誤謬를 發生시킨다。

4. instanceof演算子에 對하여 說明하시오。

A instanceof B = A(客體) instance of B(클래스名)
// → true of false (演算子이기 때문이다)

if (ref instanceof ClassName)

/*
ref가 ClassName클래스의 인스턴스를 參照하면 true를 返還
ref가 ClassName을 相續(繼承)하는 클래스의 인스턴이면 true를 返還
	→ 앞에 오는 게 뒤에 오는 것을 相續(繼承)하면 true
*/
public class InstanceOf {

	public static void main(String[] args) {
		Box box1 = new Box();
		PaperBox box2 = new PaperBox();
		GoldPaperBox box3 = new GoldPaperBox();
		
        /*
        box1은 Box클래스를 參照、Box客體를 生成
        box2는 PaperBox클래스를 參照、PaperBox客體를 生成
        box3은 GoldPaperBox클래스를 參照、GoldPaperBox客體를 生成
        */
		
		wrapBox(box1);
		wrapBox(box2);
		wrapBox(box3);
	}

	public static void wrapBox(Box box) {

		if (box instanceof GoldPaperBox) {
			/*
            GoldPaperBox gold = (GoldPaperBox) box;
			gold.goldWrap();
			
			上段의 코드와 下段의 코드는 同一하다。
            그러나 下段의 코드가 더 많이 使用된다。
			*/
			((GoldPaperBox) box).goldWrap();
		} else if (box instanceof PaperBox) {
			((PaperBox) box).paperWrap();
		} else {
			box.simpleWrap();
		}
	}
}
/*
box1 → GoldPaperBox(x) → PaperBox(x) → box.simpleWrap();
box2 → GoldPaperBox(x) → PaperBox(o) → ((PaperBox) box).paperWrap();
box3 → GoldPaperBox(o) → ((GoldPaperBox) box).goldWrap();

型變換을 시켜준 理由
→ box客體가 어떤 타입을 參照하는지 모르기 때문에 型變換으로 明視해 준 것임。
*/

5. 多形性(多態性)과 函數오버라이드를 適用하여、下段의 클래스들을 完成하시오。

  • Main클래스
public class ShapeTest {

	public static void main(String[] args) {

		double sumArea = 0;

		Circle circle = new Circle(10);
		Triangle triangle = new Triangle(10, 10);
		Rectangle rectangle = new Rectangle(10, 10);

		sumArea = circle.getArea() + triangle.getArea() + rectangle.getArea();
		System.out.println("顧客님 넓이는 " + sumArea + "입니다。");

		//
		sumArea = 0;
		Shape[] shape = {new Circle(10), new Triangle(10, 10), new Rectangle(10, 10)};
		
		for (Shape s : shape) {
			sumArea += s.getArea();
		}
		
		System.out.println("顧客님 넓이는 " + sumArea + "입니다。");
		
	}

}
  • Shape
class Shape {

	public double getArea() {
		return 0.0;
	}
	
}
  • Circle
class Circle extends Shape {

	private int r;

	public Circle(int r) {
		this.r = r;
	}
	
	@Override
	public double getArea() {
		return r * r * Math.PI;
	}

}
  • Triangle
class Triangle extends Shape {

	private int width, height;

	public Triangle(int width, int height) {
		this.width = width;
		this.height = height;
	}

	@Override
	public double getArea() {
		return width * height / 2.0;
	}

}
  • Rectangle
class Rectangle extends Shape {
	
	private int width, height;
	
	public Rectangle(int width, int height) {
		this.width = width;
		this.height = height;
	}
	
	@Override
	public double getArea() {
		return width * height;
	}
	
}

6. 下段의 內容을 프로그래밍 하시오。

겜블링 게임에 參與할 選手 數字 → 3
1番째 選手의 이름 → 金珉喜
2番째 選手의 이름 → 南春香
3番째 選手의 이름 → 李佳恩
[金珉喜] : <Enter>
3 3 2 아쉽군요!
[南春香] : <Enter>
3 3 2 아쉽군요!
[李佳恩] : <Enter>
1 1 1 李佳恩님이 이겼습니다!

profile
移轉했읍니다。

2개의 댓글

comment-user-thumbnail
2022년 11월 9일

릴파여신

1개의 답글