instanceof 객체관계를 확인
Posted 2015. 4. 2. 15:10void 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++;
}
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 비교를 해야 했을 수도.
'Java !!!' 카테고리의 다른 글
인터페이스를 허용의 의미로 사용한 예 (0) | 2015.04.03 |
---|---|
인터페이스 활용 예 (0) | 2015.04.03 |
상속 예시 한개 (0) | 2015.04.01 |
상속 (0) | 2015.04.01 |
자바 random (0) | 2015.04.01 |
- Filed under : Java !!!