λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

ν”„λ‘œκ·Έλž˜λ° 곡뢀/Java

[15일차] μ°Έμ‘°ν˜• ν˜•λ³‘ν™˜(Casting), instanceof μ—°μ‚°μž

Ex59 ~ Ex65

 

μ°Έμ‘°λ³€μˆ˜μ˜ ν˜•λ³€ν™˜
- 상속 관계에 μžˆλŠ” 클래슀끼리의 ν˜•λ³€ν™˜ κ°€λŠ₯
- A클래슀 > B클래슀

1. μ—…μΊμŠ€νŒ…, Up Casting
- μ•”μ‹œμ μΈ ν˜•λ³€ν™˜(ν˜•λ³€ν™˜ μƒλž΅ κ°€λŠ₯)
- μžμ‹ ν΄λž˜μŠ€ > (ν˜•λ³€ν™˜) > λΆ€λͺ¨ ν΄λž˜μŠ€
- 100% μ•ˆμ „

2. λ‹€μš΄μΊμŠ€νŒ…, Down Casting
- λͺ…μ‹œμ μ΄ ν˜•λ³€ν™˜(ν˜•λ³€ν™˜ μƒλž΅ λΆˆκ°€)
- λΆ€λͺ¨ ν΄λž˜μŠ€ > (ν˜•λ³€ν™˜) > μžμ‹ ν΄λž˜μŠ€
- 100% λΆˆκ°€λŠ₯
- μ½”λ“œμ˜ μœ μ—°μ„± λΆ€μ—¬ > νŒμ²˜λŸΌ μ‚¬μš© 

 

 

1.μ—…μΊμŠ€νŒ…

λΆ€λͺ¨ν΄λž˜μŠ€ = μžμ‹ν΄λž˜μŠ€;

Parent p1 = new Parent();
p1.a = 10;
p1.b = 20;
		
Child c1 = new Child();
c1.a = 10;
c1.b = 20;
c1.c = 30;
c1.d = 40;

Parent p2;
Child c2;
		
c2 = new Child(); //원본
		
//μ—…μΊμŠ€νŒ…
//Parent = Child
//λΆ€λͺ¨ν΄λž˜μŠ€ = μžμ‹ν΄λž˜μŠ€
p2 = c2; //μƒλž΅(μ•„λž˜μ™€ λ˜‘κ°™μŒ)
p2 = (Parent)c2; //μ›ν˜•
		
//볡사 μ™„λ£Œ > μ œλŒ€λ‘œ 볡사 μ™„λ£Œ 검증? > λ³΅μ‚¬λ³Έμ˜ 멀버가 λͺ¨λ‘ μ˜¬λ°”λ₯΄κ²Œ μ‚¬μš©λ˜λŠ”μ§€ 확인?
p2.a = 10;
p2.b = 20;

 

2. λ‹€μš΄μΊμŠ€νŒ…

μžμ‹ = λΆ€λͺ¨;

100% λΆˆκ°€λŠ₯

Parent p4;
Child c4;
		
c4 = new Child();

//μ—…μΊμŠ€νŒ…(100%)
p4 = c4;

Child c5;
		
//μžμ‹ = λΆ€λͺ¨
//λ‹€μš΄ μΊμŠ€νŒ…(100% λΆˆκ°€λŠ₯)
c5 = (Child)p4;

c5.a = 10;
c5.b = 20;
c5.c = 30;
c5.d = 40;

	}//main
	
}//main class


class Parent {
	public int a;
	public int b;
}

class Child extends Parent {
	public int c;
	public int d;
}

μ˜ˆμ‹œ 1.ν”„λ¦°ν„°

더보기
package com.test.obj.cast;

public class Ex60_Casting {

	public static void main(String[] args) {
		
		//상황] λŒ€λ¦¬μ  운영 > ν”„λ¦°ν„° 판맀 
		//1. HP600 x 5λŒ€, LG500 x 3λŒ€
		//2. 주기적으둜 μ œν’ˆ 점검
		
		//λͺ©μ ] μ œν’ˆ 점검 > 효율적!!
		//변경사항] 재고 증가 HP600 > 500, LG500 > 300 > m2
		//변경사항] λΈŒλžœλ“œ 증가 > BenQ, ASUS, MS... > m3
		//변경사항] 점검 κΈ°λŠ₯ > print(), selfTest(), call()
		
		//m1();
		//m2();
		//m3(); //**** 정리 μ€‘μš”
		
		
		
	}//main
	
	private static void m3() {
		
		//Case 3. 
		
		//ν˜•λ³€ν™˜ μ‚¬μš©
		
		HP600 hp = new HP600("black", 250000, "ink");
		
		LG500 lg = new LG500("white", 350000, "lazer");
		
		Printer p1;
		
		//λΆ€λͺ¨ = μžμ‹
		//μ—…μΊμŠ€νŒ…
		p1 = hp;
		
		//μ—…μΊμŠ€νŒ…
		Printer p2 = lg;
		
		Printer[] list = new Printer[2];
		list[0] = hp;
		list[1] = lg;
		
		//list > HP와 LGλ₯Ό ν•œλ²ˆμ— 담은 λ°°μ—΄
		
		
		
		Printer[] ps = new Printer[8];
		
		for (int i=0; i<ps.length; i++) {
			
			if (i < 5) {
				ps[i] = new HP600("black", 250000, "ink");
			} else {
				ps[i] = new LG500("white", 350000, "lazer");
			}
			
		}
		
		//점검
		for (int i=0; i<ps.length; i++) {
			
			ps[i].print();
			
			//λ‹€μš΄ μΊμŠ€νŒ… > ν‰μ†Œμ—λŠ” ν˜•μ œλ“€κ³Ό 같이 λΆ€λͺ¨ 배열에 λ„£μ–΄μ„œ κ΄€λ¦¬ν•˜λ‹€κ°€.. μžμ‹λ§Œμ΄ κ°€μ§€κ³  μžˆλŠ” 고유 κΈ°λŠ₯을 μ‚¬μš©ν•΄μ•Ό ν• λ•Œ > λ‹€μš΄ μΊμŠ€νŒ…μ„ μ‚¬μš©ν•΄μ„œ μ›λž˜ νƒ€μž… μ°Έμ‘° λ³€μˆ˜λ‘œ ν˜•λ³€ν™˜ 
			if (ps[i] instanceof HP600) {
				HP600 hp1 = (HP600)ps[i];
				hp1.selfTest(); 
			} else if (ps[i] instanceof LG500){
				LG500 lg1 = (LG500)ps[i];
				lg1.call();
			}
			
			
			//μ—°μ‚°μž
			//객체 instanceof ν΄λž˜μŠ€μ΄λ¦„ : μ•žμ—μžˆλŠ” 객체λ₯Ό ν΄λž˜μŠ€μ— 넣어도 λ˜λŠ”μ§€?(ν˜•λ³€ν™˜ν•΄μ„œ κ°€λŠ₯ν•œκ²½μš° 포함)
			//System.out.println(ps[i] instanceof HP600);
			
			
		}
		
		
	}

	private static void m2() {
		
		//Case 2.
		
		//재고 확보
		HP600[] hps = new HP600[5];
		
		for (int i=0; i<hps.length; i++) {
			hps[i] = new HP600("black", 250000, "ink");
		}
		
		LG500[] lgs = new LG500[3];
		
		for (int i=0; i<lgs.length; i++) {
			lgs[i] = new LG500("white", 350000, "lazer");
		}
		
		//점검 > λΈŒλžœλ“œ 1개 > 루프 1개
		for (int i=0; i<hps.length; i++) {
			hps[i].print();
			hps[i].selfTest();
		}
		
		for (int i=0; i<lgs.length; i++) {
			lgs[i].print();
			lgs[i].call();
		}
		
	}

	private static void m1() {
		
		//Case 1.
		
		//재고 확보
		HP600 hp1 = new HP600("black", 250000, "ink");
		HP600 hp2 = new HP600("black", 250000, "ink");
		HP600 hp3 = new HP600("black", 250000, "ink");
		HP600 hp4 = new HP600("black", 250000, "ink");
		HP600 hp5 = new HP600("black", 250000, "ink");
		
		LG500 lg1 = new LG500("white", 350000, "lazer");
		LG500 lg2 = new LG500("white", 350000, "lazer");
		LG500 lg3 = new LG500("white", 350000, "lazer");
		
		//점검 x 반볡
		hp1.print();
		hp2.print();
		hp3.print();
		hp4.print();
		hp5.print();
		
		lg1.print();
		lg2.print();
		lg3.print();
		
		
	}

}//main class


class Printer {
	
	private String color;
	private int price;
	private String type;
	
	public Printer(String color, int price, String type) {
		super();
		this.color = color;
		this.price = price;
		this.type = type;
	}
	
	//λ°”μ§€ 사μž₯ > μΈν„°νŽ˜μ΄μŠ€ > ν†΅λ‘œ μ—­ν• 
	public void print() {
		
	}
	
}


class HP600 extends Printer {
	
//	private String color;
//	private int price;
//	private String type;
	
	public HP600(String color, int price, String type) {
		super(color, price, type);
//		this.color = color;
//		this.price = price;
//		this.type = type;
	}

//	@Override
//	public String toString() {
//		return "HP600 [color=" + color + ", price=" + price + ", type=" + type + "]";
//	}
	
	public void print() {
		System.out.println("HP600으둜 좜λ ₯ν•©λ‹ˆλ‹€.");
	}
	
	public void selfTest() {
		System.out.println("HP600의 μžκ°€μ§„λ‹¨μ„ μ‹œμž‘ν•©λ‹ˆλ‹€.");
	}
	
}

class LG500 extends Printer {
	
//	private String color;
//	private int price;
//	private String type;
	
	public LG500(String color, int price, String type) {
		super(color, price, type);
//		this.color = color;
//		this.price = price;
//		this.type = type;
	}

//	@Override
//	public String toString() {
//		return "LG500 [color=" + color + ", price=" + price + ", type=" + type + "]";
//	}
	
	public void print() {
		System.out.println("LG500으둜 좜λ ₯ν•©λ‹ˆλ‹€.");
	}
	
	public void call() {
		System.out.println("인곡지λŠ₯ AI와 μ—°κ²°ν•©λ‹ˆλ‹€.");
	}
	
}

 

μ˜ˆμ‹œ2. 건전지

더보기
package com.test.obj.cast;

public class Ex61_Casting {

	public static void main(String[] args) {
		
		
		//μ „μž μ œν’ˆ μ‚¬μš© > 배터리 ν•„μš”
		
		Duracell b1 = new Duracell();
		
		//μ—…μΊμŠ€νŒ…
		setPower(b1); //main이 static이라 this λͺ»μ”€(static λ©€λ²„λ§Œ 호좜 κ°€λŠ₯) > κ·Έλž˜μ„œ setPower은 static λ©”μ†Œλ“œμ—¬μ•Όν•¨
		
		//setPower static μ•ˆλΆ™μ΄κ³  μ‹Άλ‹€λ©΄..(잘 μ•ˆμ“°λŠ” 방법)
//		Ex61_Casting ex61 = new Ex61_Casting();
//		ex61.setPower(b1);
		
		//μ‹œκ°„μ΄ 흘러..
		Neo b2 = new Neo();
		
		//μ—…μΊμŠ€νŒ…
		setPower(b2);
		
		
		//건전지 자판기
		Duracell b3 = getPower();
		
		setPower(b3);
		
		
		
		//μ‚¬μš©μž μž…μž₯
		//- 건전지λ₯Ό μ‚¬μš©ν•œλ‹€."
		//- 듀라셀이든 λ„€μ˜€λ“  μ€‘μš”ν•˜μ§€ μ•ŠμŒ.. μ „μžμ œν’ˆμ΄ λ™μž‘λ§Œ 되면 되기 λ•Œλ¬Έμ—.. 건전지면 λœλ‹€.
		//좔상화 > ν–‰λ™μ˜ λ‹¨μˆœν•¨
		Battery b4 = getPower(1); //1.듀라셀, 2.λ„€μ˜€
		
		setPower(b4);
		
		
		
	}//main
	
	private static Battery getPower(int sel) {
		
		if (sel == 1) {
			return new Duracell();
		} else {
			return new Neo();
		}
		
	}

	private static Duracell getPower() {
		
		Duracell b = new Duracell();
		
		return b;
	}

	private static void setPower(Battery b1) {
		
		System.out.println("μ „μž μ œν’ˆμ΄ λ™μž‘ν•©λ‹ˆλ‹€.");
		
	}

}//main class

class Battery {
	public String price;
	public int capacity;
}

class Duracell extends Battery {
	public String color;
}

class Neo extends Battery {
	public int weight;
}

instanceof μ—°μ‚°μž

- μ°Έμ‘°λ³€μˆ˜μ˜ ν˜•λ³€ν™˜ κ°€λŠ₯μ—¬λΆ€ 확인에 μ‚¬μš©. κ°€λŠ₯ν•˜λ©΄ true λ°˜ν™˜

- ν˜•λ³€ν™˜ 전에 λ°˜λ“œμ‹œ instanceof둜 확인해야 함

 

void doWork(Car c) {
	if (c instanceof FireEngine) { //1. ν˜•λ³€ν™˜μ΄ κ°€λŠ₯ν•œμ§€ 확인
    	FireEngine fe = (FireEngine)c; //2. ν˜•λ³‘ν™˜	
        fe.water();
        ...