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();
...
'νλ‘κ·Έλλ° κ³΅λΆ > Java' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[16μΌμ°¨] μΆμ ν΄λμ€(abstract class), μμ, μ΄κ±°ν(enumeration) (0) | 2023.03.04 |
---|---|
[15μΌμ°¨] μΈν°νμ΄μ€(interface) (0) | 2023.03.02 |
[14μΌμ°¨] μμ(Inheritance), λ©μλ μ€λ²λΌμ΄λ©(Method Overriding), static, super (0) | 2023.02.21 |
[13μΌμ°¨] ν΄λμ€(2), μμ±μ (0) | 2023.02.21 |
[13μΌμ°¨] λλ―Έ λ°μ΄ν°(κ°μ λ°μ΄ν°) λ§λ€κΈ° (1) | 2023.02.21 |