본문 바로가기

JAVA/되새김질

객체지향 복습(3) ─1:1 (Obj vs Obj) + 객체 사용


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class Friend99 {
    String name;
    String date;
    
    Friend99(String name,String date){
        this.name=name;
        this.date=date;
    }
    
    void show() {
        System.out.println(name+"을 "+date+"에서 만났다.");
    }
}
class YoungerTime99 {
    Friend99 f;
    String name;
    
    YoungerTime99(String name){
        this.name=name;
    }
    
    void meetFriend(Friend99 f) {
        this.f=f;
    }
    void memory() {
        if(f!=null) {
            System.out.println(name+"는 "+f.name+"을 "+f.date+" 만났습니다.");
        }
        else {
            System.out.println("친구의 기억이 없습니다.");
        }
    }
}
 
public class A11_Practice {
    
    public static void main(String [] args) {
        Friend99 f9 = new Friend99("쿵야","아까");
        
        YoungerTime99 yt = new YoungerTime99("상심이");
        f9.show();
        yt.memory();
        yt.meetFriend(f9);
        yt.memory();
        yt.meetFriend(new Friend99("방울이","어제"));
        yt.memory();
    }
}
cs

 

1
2
3
4
 
쿵야을 아까에서 만났다.
친구의 기억이 없습니다.
상심이는 쿵야을 아까 만났습니다.
상심이는 방울이을 어제 만났습니다.
 
cs

Friend99 f의 쓰임을 살펴보자.

meetFriend 메서드의 매개변수로 쓰였고 

memory 메서드에서 Friend99의 필드를 직접 데려오기 위해 쓰였다.

 

메인메서드를 보자.

f9.show(); -> 쿵야을 아까에서 만났다.

f9에 저장한 값을 내왔다.

 

yt.memory(); -> 친구의 기억이 없습니다.

memory메서드를 불러왔지만 f9값의 주인인 meetFriend 메서드를 불러오지 않았기에 빈값이 나온다

 

yt.meetFriend(f9);

yt.memory(); -> 상심이는 쿵야을 아까 만났습니다.

meetFriend(f9)를 불러옴으로서 Friend99 객체를 온전히 사용할 수 있게 되었다.

 

다시말해

외부클래스를 필드로 가져온 경우 그것을 메서드의 매개변수값으로 사용할 수 있고,

메인메서드에서 해당 메서드를 불러와 사용할 수 있다.

왠지

Friend99 f(f9) 이 forgine key 역할과 비슷한 느낌.

두 클래스를 이어주면서 class Friend99에선 primary key. 

 

yt.meetFriend(new Friend99("방울이","어제")); 생성자의 형태에 맞춰 메인메서드에서 초기화 할 수 있다.

 

메인메서드에서도 일반 필드객체 불러오듯 해볼까?

getF를 해보니 주소값이 나온다. Friend99 f가 담긴 주소값이.

이 주소값은 f9의 주소값과 다르다.

 

둘은 분명히 같은 설계도로 만들어진 똑같은 건물이다. 창문방향, 크기, 높이 등등 모든게 똑같다.

아파트 1단지와 2단지처럼.

하지만 두 건물은 주소는 다르지. 이와 마찬가지다.

f9와 Friend99 f는 하나의 class Friend99 로 만들어진 객체지만 다른 주소를 갖는 이유가 이것이다.

 

 

 

 

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Product88 {
    String name;
    int price;
    int cnt;
    
    Product88(String name, int price, int cnt) {
        this.name=name;
        this.price=price;
        this.cnt=cnt;
    }
    void mess1() {
        System.out.println("물건 "+name+"을(를) "+price+"원에 "+cnt+"개 구매");
    }
 
}//end of Product88;
class Mart99 {
    String name;
    Product88 p;
    
    Mart99(String name){
        this.name=name;
    }
    
    void mess1(Product88 p) {
        this.p=p;
    }
    void mess2() {
        if(p!=null) {
            System.out.print(name+"에서 ");
            p.mess1();
        }
        else {
            System.out.println("구매한 물품이 없습니다.");
        }
    }
}
public class A11_Practicee {
    public static void main(String [] args) {
        Product88 p8 = new Product88("빼빼로",1500,3);
        p8.mess1();
        
        Mart99 m9 = new Mart99("롯데마트");
        m9.mess1(p8);
        m9.mess2();
        m9.mess1(new Product88("샌드위치",2900,1));
        m9.mess2();
    }
}
cs

 

1
2
3
4
물건 빼빼로을(를) 1500원에 3개 구매
롯데마트에서 물건 빼빼로을(를) 1500원에 3개 구매
롯데마트에서 물건 샌드위치을(를) 2900원에 1개 구매
 
cs

 

Mart99 클래스를 들여다보자.

Product88 클래스를 필드마냥 가져왔다.

Product88은 name,price,cnt를 매개변수로 초기화 되었으며 mess1 메서드를 가지고 있는 클래스다.

이런 애를 변수 p를 써서 Mart99의 필드로 가져왔다.

어떻게 쓸까?

 

여기서는 mess1메서드의 매개변수로 썼다. 이것은 위에 쿵야와 상심이가 만난 것과 같은 방식이다.

중요한 것은

 mess2에서 p.mess1로 Product88의 메서드를 가져오기 위해 p를 사용했다는 것.

정말 필드처럼 썼다.

대신 사용법은 객체 가져다 쓰는것처럼 참조변수를 불러와 쓰지만 사용처는 필드와 같다.