Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- mariadb
- 개발자
- localStorage
- partitioning
- node.js
- SSE
- PostgreSQL
- Node.js기본
- 열공하자
- 성장기
- 실시간알림
- 파티셔닝
- EventSource
- frontend
- Lag
- Partition
- node.js란
- ServerSentEvent
Archives
- Today
- Total
써치킴의 우당탕탕 개발 블로그
[Java][Ch2] has-a 상속 본문
상속 (inheritance)
-
클래스를 조립하여 프로그램을 만드는 것
has - a : 갖다
- 사용 : 수정이 필요없는 정확히 맞는 부품이 이미 존재
-
특징
-
composition
-
association
-
aggregation
-
-
구현 : 부모클래스를 멤버변수로 포함
public class Child{
public Perents parents
}
composition
-
구현 : 자식이 부모클래스를 멤버변수로 포함
public class Child{
public Perents parents
}
-
부모가 여러 클래스일 수 있다. (부모클래스 수에 대한 제약X)
-
자식이 멤버변수에서 부모를 참조하고 생성자함수에서 객체를 생성한다.
AA.java
public class AA {
private int aa;
private BB pb; // BB클래스(부모 클래스)가 멤버변수로 들어옴 -> 부모객체
// 생성자 함수 오버로드
public AA(){
this(0);
}
public AA(int aa){ // 꼭 생성자함수에서 부모클래스의 위치값을 저장해야함!!!!
this.aa = aa;
pb = new BB(); // 생성자함수에서 부모클래스의 위치값(참조값)을 저장함(객체화)
}
public void setAa(int aa){
this.aa = aa;
}
public int getAa(){
return aa;
}
public void printA(){
System.out.println("AA클래스의 printA 메서드를 실행하였습니다.");
System.out.println("AA클래스의 aa = " + this.aa);
// /*pb.bb=10;*/ // bb변수는 private변수이므로 불가
pb.setBb(200); // Setter로 접근함
pb.printB(); // 부모의 함수 호출
}
}
BB.java
public class BB { // 부모 클래스
private int bb;
public BB(){
this(0);
}
public BB(int bb){
this.bb = bb;
}
public void setBb(int bb){
this.bb = bb;
}
public int getBb(){
return bb;
}
public void printB(){
System.out.println("BB클래스의 printB 메서드를 실행하였습니다.");
System.out.println("BB클래스의 bb = " + this.bb);
}
}
MainEx.java
public class MainEx {
public static void main(String[] args) {
AA ca = new AA();
ca.setAa(100);
ca.printA();
}
}
Association
1. 생성자 함수에서 인자전달(constructor injection)
AA.java
public class AA {
private int aa;
private BB pb; // BB클래스(부모 클래스)가 멤버변수로 들어옴(부모객체)
// 생성자 함수 오버로드
public AA(){ // 기본생성자
this(0, null);
}
public AA(int aa, BB pb){ // 1. 생상자 함수에서 인자전달
this.aa = aa;
this.pb = pb; // 생성자함수에서 부모클래스의 위치값(참조값)을 저장함(객체화) -> composition 방식
}
public int getAa(){
return aa;
}
public void printA(){
System.out.println("AA클래스의 printA 메서드를 실행하였습니다.");
System.out.println("AA클래스의 aa = " + this.aa);
pb.setBb(200); // Setter로 접근함
pb.printB(); // 부모의 함수 호출
}
}
BB.java
public class BB {
private int bb;
public BB(){
this(0);
}
public BB(int bb){
this.bb = bb;
}
public void setBb(int bb){
this.bb = bb;
}
public int getBb(){
return bb;
}
public void printB(){
System.out.println("BB클래스의 printB 메서드를 실행하였습니다.");
System.out.println("BB클래스의 bb = " + this.bb);
}
}
MainEx.java
public class MainEx {
public static void main(String[] args) {
BB pb = new BB(); // AA 자식클래스에서 BB클래스를 참조하므로 BB 클래스 객체 선언을 먼저 해줘야 한다.
AA ca = new AA(100, pb); // 생성자 인젝션을 사용할 경우
ca.printA();
}
}
2. Setter 함수에서 인자 전달(setter injection)
AA.java
public class AA {
private int aa;
private BB pb; // BB클래스(부모 클래스)가 멤버변수로 들어옴(부모객체)
public void setPb(BB pb) { // 2. setter에서 인자 전달
this.pb = pb;
}
public void setAa(int aa){
this.aa = aa;
}
public int getAa(){
return aa;
}
public void printA(){
System.out.println("AA클래스의 printA 메서드를 실행하였습니다.");
System.out.println("AA클래스의 aa = " + this.aa);
pb.setBb(200); // Setter로 접근함
pb.printB(); // 부모의 함수 호출
}
}
BB.java
public class BB {
private int bb;
public BB(){
this(0);
}
public BB(int bb){
this.bb = bb;
}
public void setBb(int bb){
this.bb = bb;
}
public int getBb(){
return bb;
}
public void printB(){
System.out.println("BB클래스의 printB 메서드를 실행하였습니다.");
System.out.println("BB클래스의 bb = " + this.bb);
}
}
MainEx.java
public class MainEx {
public static void main(String[] args) {
BB pb = new BB(); //세터 인젝션 사용할 경우
AA ca = new AA();
ca.setPb(pb);
ca.setAa(100);
ca.printA();
// (인젝션 : 집어넣는다)
// 생성자 인젝션 or 세터 인젝션 중 하나만 쓰면 됨
// 주로 세터 인젝션 사용
}
}
composition_aggregation
AA.java
public class AA { // 자식 1, 부모 3 composition 방식의 aggregation
private int aa;
private BB[] pbs;
public AA(){
this(0);
}
public AA(int aa){
this.aa = aa;
pbs = new BB[3]; // 배열만 만들어짐
for (int i = 0; i < pbs.length; i++) { // 각 배열에 객체 생성 및 참조
BB pb = new BB(); // 객체 생성
pbs[i] = pb; // 참조
}
}
public void setAa(int aa){
this.aa = aa;
}
public int getAa(){
return aa;
}
public void printA(){
System.out.println("AA클래스의 printA 메서드를 실행하였습니다.");
System.out.println("AA클래스의 aa = " + this.aa);
for (int i = 0; i < pbs.length; i++) { // 입력
pbs[i].setBb(100*(i+1));
}
for (int i = 0; i < pbs.length; i++) { // 출력
pbs[i].printB();
}
}
}
BB.java
public class BB {
private int bb;
public BB(){
this(0);
}
public BB(int bb){
this.bb = bb;
}
public void setBb(int bb){
this.bb = bb;
}
public int getBb(){
return bb;
}
public void printB(){
System.out.println("BB클래스의 printB 메서드를 실행하였습니다.");
System.out.println("BB클래스의 bb = " + this.bb);
}
}
MainEx.java
public class MainEx {
public static void main(String[] args) {
AA ca = new AA(100); // 생성자 함수 변수 aa에 100 넣음
ca.printA();
}
}
'더 자세하게 Java' 카테고리의 다른 글
[Java][Ch2] 메서드동적바인딩 (0) | 2022.05.13 |
---|---|
[Java][Ch2] is-a 상속 (0) | 2022.05.13 |
[Java][Ch2] 캡슐화 (0) | 2022.05.13 |
[Java][Ch2] 객체지향_클래스 (0) | 2022.05.13 |
[Java][Ch1] 참조타입 (0) | 2022.05.13 |
Comments