Operation
instanceof
์ฐธ์กฐ๋ณ์๊ฐ ์ฐธ์กฐํ๋ ์ธ์คํด์ค์ ์ค์ ํ์ ์ ์์๋ณด๊ธฐ ์ํด ์ฌ์ฉ
class Car {
String color;
int door;
}
class Sedan extends Car {
void driveSedan() {
System.out.println("Start Sedan");
}
}
class SportsCar extends Car {
void driveSportCar() {
System.out.println("Start Sports Car");
}
}
Sedan sedan = new Sedan();
SportsCar sportsCar = new SportsCar();
isCar(sedan);
isCar(sportsCar);
void isCar(Car car) {
if (car instanceof Sedan) {
Sedan s = (Sedan) car;
s.driveSedan();
} else if (car instanceof SportsCar) {
SportsCar p = (SportsCar) car;
p.driveSportCar();
}
}
๊ฒฐ๊ณผ
// Start Sedan
// Start Sports Car
์ฐธ๊ณ ์๋ฃ