import Exam_1.UI코드분리하기.Exam;
public class Program {
public static void main(String[] args) {
NewlecExam exam = new NewlecExam(1, 1, 1, 1);//생성시 직접입력 가능해짐
// exam.setMath(10); //생성자가 있어서 필요 따로 입력할 필요 없게됨
// exam.setKor(10);
// exam.setEng(10);
// exam.setCom(10);
System.out.println(exam.total());
System.out.println(exam.avg());
}
}
import Exam_1.UI코드분리하기.Exam;
public class NewlecExam extends Exam {
private int com;
public int getCom() {
return com;
}
public void setCom(int com) {
this.com = com;
}
public NewlecExam() { //매개변수 입력없을시 실행됨
this(0,0,0,0);// 밑에있는 오버로드가 우선순위로 실행됨 this는 밑의 오버로드를 가리킴
}
public NewlecExam(int kor, int eng, int math, int com) { //자식 생성자 오버로드
super(kor, eng, math); //직접 초기화하려고 하지말고 부모의 생성자를 활용
this.com = com;
}
@Override
public int total() {
return super.total()+com;//부모의 total (국어, 영어, 수학의 합)에 com만 더 해주면됨
}
@Override
public float avg() {
return total()/4.0f; //여기서 total은 자식 total
}
}
'Java 코드연습 > 상속' 카테고리의 다른 글
[뉴렉처] Override(우선순위가 높은) 메소드 (0) | 2021.10.30 |
---|---|
[뉴렉처] IS A 상속 (0) | 2021.10.30 |
[뉴렉처] 코드 재사용이란? (0) | 2021.10.29 |
[뉴렉처] Has A 상속 (0) | 2021.10.28 |