Notice
Recent Posts
Recent Comments
«   2024/09   »
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
Archives
Today
Total
관리 메뉴

충분히 쌓여가는

포함(Composite) 관계 본문

Java/객체지향

포함(Composite) 관계

빌드이너프 2023. 6. 4. 23:23

포함(Composite)

클래스의 멤버로 참조변수를 선언하는 것

 

1번은 Point 클래스와 관계없음

2번은 Circle가 Point를 포함하는 관계

1번과 같이 해도 되지만 2번과 같이 해도됨

 

 

차라는 클래스를 만들때 엔진과 문같은 부품을 포함관계로 만들면 복잡도가 낮아지게 된다

class Car {
    Engine e = new Engine();
    Door[] d = new Door[4];
}

 

코드

class MyPoint {
    int x;
    int y;
}

class Circle  { // 포함
    MyPoint p = new MyPoint();
    int r;
}


public class InheritanceTest {
    public static void main(String[] args) {
        Circle c = new Circle();
        c.p.x = 1;
        c.p.y = 2;
        c.r = 3;
        System.out.println(c.p.x);
        System.out.println(c.p.y);
        System.out.println(c.r);
    }
}

'Java > 객체지향' 카테고리의 다른 글

단일 상속(Single Inheritance)  (0) 2023.06.05
상속이냐 포함이냐 (클래스간의 관계 설정)  (1) 2023.06.05
상속(Inheritance)  (0) 2023.06.04
변수의 초기화  (0) 2023.06.01
생성자 this()와 참조변수 this  (0) 2023.06.01