본문 바로가기

명사 美 비격식 (무리 중에서) 아주 뛰어난[눈에 띄는] 사람[것]

JAVA

this 와 super

this  = 부모의 멤버변수
super = 생성자(부모)

 

this
부모의 멤버변수
getter setter에서 많이볼 수 있는데
생성자의 멤버변수를 가리킬때 자주사용된다.

public class MyClass {
    private int num;
    public MyClass(int num) {
        this.num = num;
    }
    public void printNum() {
        System.out.println("num = " + this.num);
    }
}

 

super
부모생성자

부모 클래스 또는 인터페이스에서 생성자를 호출하는 용도

부모에게 상속을 받고 생성자를 만들때 super()는 기본값으로, 보통 생략되어있다.

부모의 정보가 필요할때만 가져다 쓰면 되겠다.

public class Animal {
    private String name = "이름";
}

public class Dog extends Animal {
    public Dog(String name) {
        super(name);
    }
}

 

'JAVA' 카테고리의 다른 글

덮어쓰는 annotation, @Override  (0) 2023.03.16
Class 형변환  (0) 2023.03.16
상속받다, extends  (0) 2023.03.16
날짜형식 정하기, SimpleDateFormat()  (0) 2023.03.16
날짜 출력하기, Date  (0) 2023.03.16