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

์ฐธ๊ณ ์ž๋ฃŒ

https://arabiannight.tistory.com/entry/301