Java !!!

instanceof 객체관계를 확인

asdwasd12as 2015. 4. 2. 15:10
void buy(Food f){
  if( money < f.price ){
   System.out.println("집에가시오");
   return;
  }
  money -= f.price;
  point += f.bonusPoint;
  f.sellCnt++;
  System.out.println( f + "(" + f.price + "원)");
  cart[cnt++] = f;
  
  // 과일, 음료, 과자의 갯수 증가
  // instanceof 연산자 : 참조변수와 객체관게를 알려준다.
  if( f instanceof Fruit ) FruitCnt++;
  if( f instanceof Drink ) drinkCnt++;
  if( f instanceof Snack ) snackCnt++;
 }

f = Food의 객체 이다.

넘어온 객체가 A의 객체인지, B의 객체인지 확인가능함!!

instanceof 유용할듯.

저게 아니였다면 string 비교를 해야 했을 수도.