2013년 8월 2일 금요일

java inheritence and composition(자바 상속 컴포지션)

상속과 컴포지션은 동전의 양면과 같이 유사하게 서로에게 관련이 있다. 
 - 상속은 마치 양파가 여러 껍질로 이루어진 것과 같이 계층화된 객체
 - 컴포지션은 여러 재료(객체)가 한데 뭉쳐서 만들어진 죽
 - 컴포지션은 개체들간의 'has a' 관계, 상속은 ‘is a’관계
 - 상속과 컴포지션은 상호 배타적이지 않으며 개발자는 이 둘을 같이 사용한다.

오라클자바커뮤니티에서 설립한 오엔제이프로그래밍 실무교육센터
(오라클SQL, 튜닝, 힌트,자바프레임워크, 안드로이드, 아이폰, 닷넷 실무전문 강의) 



Composition Exam

class Soap {
  private String s;
  Soap() {
    System.out.println("Soap()");
    s = new String("Constructed");
  }
  public String toString() { return s; }
}

public class Bath {
  private String 
    // Initializing at point of definition:
    s1 = new String("Happy"), 
    s2 = "Happy", 
    s3, s4;
  Soap castille;
  int i;
  float toy;
  Bath() {
    System.out.println("Inside Bath()");
    s3 = new String("Joy");
    i = 47;
    toy = 3.14f;
    castille = new Soap();
  }
  void print() {
    // Delayed initialization:
    if(s4 == null)
      s4 = new String("Joy");
    System.out.println("s1 = " + s1);
    System.out.println("s2 = " + s2);
    System.out.println("s3 = " + s3);
    System.out.println("s4 = " + s4);
    System.out.println("i = " + i);
    System.out.println("toy = " + toy);
    System.out.println("castille = " + castille);
  }

  public static void main(String[] args) {
    Bath b = new Bath();
    b.print();
  }
}

댓글 없음:

댓글 쓰기