본문 바로가기

JAVA/되새김질

객체지향 복습(4) ─ 1:다 (Obj vs MultiObj)

0
1
2
3
4
5
6
7
8
9
10
11
12
13
class Mart{
  //Product prod;
    String name;
    private ArrayList<Product> plist;
 
    public Mart01(String mname) { 
        this.mname = mname; 
        plist = new ArrayListt<Product>();
    }
    public void buy(Product prod) { 
        plist.add(prod); 
    }
 
}
cs

 

Line3

  다중의 객체를 받아오기위한 ArrayList 생성.

  그 출처는 Product 클래스.

 

Line7

  생성자를 통해 초기화 처리.

 

Line10

  buy메서드를 통해 Product 객체에 여러 값을 넣을 수 있다.

  그리고 그 값들은 ArrayList인 plist에 담는다.

 

1:다 관계는 이와 같은 기본 틀이 있다.