Notice
Recent Posts
Recent Comments
Link
- Arawn's Dev Blog
- Outsider's Dev Story
- Toby's Epril
- Benelog
- NHN 개발자 블로그
- SK 플래닛 기술 블로그
- OLC CENTER
- 소프트웨어 경영/공학 블로그
- 모바일 컨버전스
- KOSR - Korea Operating System …
- 넥스트리 블로그
- 리버스코어 ReverseCore
- SLiPP
- 개발자를 위하여... (Nextree 임병인 수석)
- "트위터 부트스트랩: 디자이너도 놀라워할 매끈하고 직관…
- Learning English - The English…
- real-english.com
- 'DataScience/Deep Learning' 카테…
- Deep Learning Summer School, M…
- Deep Learning Courses
민서네집
상속 대신 Composition 으로 다른 객체의 기능 사용하는 예제 본문
너무 간단한 예제라 창피하지만...
package inheritance;
public class Rectangle {
int width = 20;
int height = 30;
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getArea() {
return width * height;
}
}
package inheritance;
public class Square {
int width = 10;
Rectangle rect;
// Square 가 Rectangle 를 상속받지 않고도 Rectangle의 logic을 사용할 수 있음.
// 상속 대신 Delegation, Composition 사용.
public int getArea() {
rect.setWidth(10);
return rect.getArea();
}
}
'Java' 카테고리의 다른 글
| AspectJ 버그 해결 (0) | 2015.03.25 |
|---|---|
| Tomcat Clustering (1) | 2015.02.04 |
| Interface로 다중상속을 대체할 수 있음을 보여주는 예제 (1) | 2015.01.07 |
| log 내용을 DB에 저장하기 (1) | 2014.12.17 |
| 코딩으로 log4j 설정 파일 읽어들여 설정 바꾸기 (PropertyConfigurator 이용) (0) | 2014.12.11 |
Comments