YBMExamConsole이 부모의 onPrint()를 재정의하기위해 부모객체가 필요하다.
오버라이드비교
onPrint()는 부모것 불러올게없다.
input()은 부모의 input()을 받은 후 진행해야한다. 즉 국,영,수를 받고 토익,a를 받도록 해야한다.(출력순서에따라)
package ex8.추상화.재사용;
import java.awt.Frame;
public class Program {
public static void main(String[] args) {
Exam exam = new YBMExam(1,1,1,1,1);
YBMExamConsole ybmConsole = new YBMExamConsole();
ybmConsole.setExam(exam);
ybmConsole.input();
ybmConsole.print();
}
}
package ex8.추상화.재사용;
import java.util.Scanner;
public class ExamConsole {
private Exam exam; //부품으로씀
private PrintListener printListener;
public PrintListener getPrintListener() {
return printListener;
}
public ExamConsole() {
}
public ExamConsole(Exam exam) {//오버로드생성자만들때 기본생성자만들어야함
super();
this.exam = exam;
}
public void setExam(Exam exam) {
this.exam = exam;
}
public void setPrintListener(PrintListener printListener) {
this.printListener = printListener;
}
protected Exam getExam() {
return exam;
}
protected void onPrint() {
}
public void print() {
System.out.println("-----------------------------");
System.out.println(" 성적 출력 ");
System.out.println("-----------------------------");
System.out.printf("kor:%d\n",exam.getKor());
System.out.printf("eng:%d\n",exam.getEng());
System.out.printf("math:%d\n",exam.getMath());
// if(printListener != null)
// printListener.onPrint();
onPrint();//호출
System.out.printf("total:%d\n",exam.total());
System.out.printf("avg:%f\n",exam.avg());
System.out.println("-----------------------------");
}
public void input() {
Scanner scan = new Scanner(System.in);
int kor;
int eng;
int math;
System.out.print("kor:");
kor = Integer.parseInt(scan.nextLine());
System.out.print("eng:");
eng = Integer.parseInt(scan.nextLine());
System.out.print("math:");
math = Integer.parseInt(scan.nextLine());
exam.setKor(kor);
exam.setEng(eng);
exam.setMath(math);
}
}
package ex8.추상화.재사용;
import java.util.Scanner;
public class YBMExamConsole extends ExamConsole{
@Override
protected void onPrint() {
YBMExam exam = (YBMExam)getExam();
System.out.printf("Toeic:%d\n",exam.getToeic());
System.out.printf("aa::%d\n",exam.getAa());
}
@Override
public void input() {
super.input(); //국영수먼저 받고
Scanner scan = new Scanner(System.in);
int toeic;
int aa;
System.out.print("toeic:");
toeic = Integer.parseInt(scan.nextLine());
System.out.print("aa:");
aa = Integer.parseInt(scan.nextLine());
YBMExam exam = (YBMExam)getExam();//부모형은 자식형으로바로안
exam.setToeic(toeic);
exam.setToeic(aa);
}
}