Multiple object using one array
조상타입의 배열에 자손들의 객체를 담을 수 있다.
Product p1 = new Tv();
Product p2 = new Computer();
Product p3 = new Audio();Product p[] = new Product[3];
p[0] = new Tv();
p[1] = new Computer();
p[2] = new Audio();class Buyer {
int money = 1000;
int bonusPoint = 0;
Product[] cart = new Product[10];
int i = 0;
void buy(Product p) {
if(money < p.price) {
System.out.println("잔액부족");
return;
}
money -= p.price;
bonusPoint += p.bonusPoint;
cart[i++] = p;
}
}Last updated