Java - 자식 클래스의 객체 초기화
- 부모를 가지는 클래스는 두개 객체를 생성한다. (super, this)
public class Main {
public static void main(String[] args) {
NewlecExam exam = new NewlecExam(0,0,0,0);
exam.setKor(10);
exam.setEng(10);
exam.setMath(10);
exam.setCom(10);
System.out.println(exam.total());
System.out.println(exam.avg());
}
}
public class NewlecExam extends Exam {
private int com ;
public NewlecExam() {
this(0,0,0,0);
}
public NewlecExam(int kor, int eng, int math, int com) {
super(kor,eng,math);
this.com=com;
}
public int getCom() {
return com;
}
public void setCom(int com) {
this.com = com;
}
@Override
public int total() {
return super.total()+com;
}
@Override
public float avg() {
return (float) (total()/4.);
}
}
- 기본 생성자를 추가한다. ( this(0,0,0,0) 셋팅 )
- 국, 영, 수, 컴 생성자 추가 -> super(kor,eng,math) 부모생성자호출
- 부모 객체는 부모 기능으로 초기화한다./ 내가 확장했던건내가 확장한다.
출처 :https://youtu.be/ElLpROe0OXg
댓글남기기