IT 개발노트

this와 super란? 본문

카테고리 없음

this와 super란?

limsungju 2019. 1. 25. 13:45

1. this와 super
1.1 this란?

- 현재 클래스의 인스턴스를 의미한다.
- 즉, 현재 클래스의 멤버변수를 지정할 때 사용한다.

this 사용예제
: 아래와 같이 현재 클래스(ParentClass)의 클래스 변수 mother와 동일한 이름의 파마리터(setMother 메서드)가 들어올 경우, 명확히 구분해 줄 수 있어야 한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class ParentClass {
    private String father;
    private String mother;
    
    public ParentClass() {
        this.father = "father";
        this.mother = "mother";
    }
    
    public ParentClass(String father, String mother) {
        this.father = father;
        this.mother = mother;
    }
    
    public String toString() {
        return this.father + "/" + this.mother;
    }
}


1.2 this( )란?
- 현재 클래스에 정의된 생성자를 부를때 사용된다.

this( ) 사용예제
: 아래와 같이 ParentClass의 생성자가 2개 있을 경우, 생성자 값이 들어오지 않을 경우 this( )메서드를 사용하여 두번째 생성자를 불러 초기화 할 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class ParentClass {
    private String father;
    private String mother;
    
    public ParentClass() {
        this("father""mother");
    }
    
    public ParentClass(String father, String mother) {
        this.father = father;
        this.mother = mother;
    }
    
    public String toString() {
        return this.father + "/" + this.mother;
    }
}


1.3 super란?
- 자식 클래스에서 상속받은 부모 클래스의 멤버변수를 참조할때 사용한다.

super 사용예제

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
public class ParentClass {
    private String father;
    private String mother;
    
    public ParentClass() {
        this("father""mother");
    }
    
    public ParentClass(String father, String mother) {
        this.father = father;
        this.mother = mother;
    }
    
    public String toString() {
        return this.father + "/" + this.mother;
    }
}
 
public class ChildClass extends ParentClass {
    private String daughter;
    private String son;
    
    public ChildClass() {
        this("daughter""son");
    }
    
    public ChildClass(String daughter, String son) {
        this.daughter = daughter;
        this.son = son;
    }
    
    public String toString() {
        return super.getFather() + "/" + super.getMother() + "/" + this.daughter + "/" + this.son;
    }
}


1.4 super( )란?
- 자식 클래스가 자신을 생성할 때 부모 클래스의 생성자를 불러 초기화 할때 사용됩니다. (기본적으로 자식 클래스의 생성자에 추가된다.)

super( ) 사용예제
: ChildClass객체 생성시(ChildClass child = new ChildClass) 호출 순서|
- ChildClass( ) 생성자 호출
- this( )에 의하여, ChildClass(String, String) 생성자 호출
- super( )에 의하여, ParentClass(String, String) 생성자 호출
- ChildClass 객체 생성 완료

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class ChildClass extends ParentClass {
    private String daughter;
    private String son;
    
    public ChildClass() {
        this("daughter""son");
    }
    
    public ChildClass(String daughter, String son) {
        //super();
        super("child.father""child.mother");
        this.daughter = daughter;
        this.son = son;
    }
    
    public String toString() {
        return super.getFather() + "/" + super.getMother() + "/" + this.daughter + "/" + this.son;
    }
}