티스토리 뷰

프로그래밍/JAVA

다형성 예제

문정환 2009. 10. 6. 10:44

class Product
{
 int price;
 int bounsPoint;
 Product(int price)
 {
  this.price=price;
  bounsPoint=(int)(price/10.0);
 }
}
class Tv extends Product
{
 Tv()
 {
  super(100);
 }
 public String toString()
 {
  return "Tv";
 }
}
class Computer extends Product
{
 Computer()
 {
  super(200);
 }
 public String toString()
 {
  return "Computer";
 }
}
class Buyer
{
 int money=1000;
 int bonusPoint=0;
 void buy(Product p)
 {
  if(money<p.price)
  {
   System.out.println("잔액이 부족하여 물건을 살수 없습니다.");
   return;
  }
  money-=p.price;
  bonusPoint+=p.bounsPoint;
  System.out.println(p+"을/를 구입하셧습니다.");
 }
}
public class PolyArgumentTest {
 public static void main(String[] args)
 {
  Buyer b=new Buyer();
  Tv tv=new Tv();
  Computer com=new Computer();
  b.buy(tv);
  b.toString();
  b.buy(com);
  b.toString();
  System.out.println("현재 남은 돈은"+b.money+"만원입니다.");
  System.out.println("현재 보너스 누적 포인트 "+b.bonusPoint+"점입니다.");
 }
}
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함