public class Score {
    private int kor;
    private int eng;
    private int math;

    public Score() {}

    public Score(int kor, int eng, int math) {
        this.kor = kor;
        this.eng = eng;
        this.math = math;
    }

    public int getKor() {
        return kor;
    }

    public void setKor(int kor) {
        this.kor = kor;
    }

    public int getEng() {
        return eng;
    }

    public void setEng(int eng) {
        this.eng = eng;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }
}
​

 

public class Student {
    private int no;
    private String name;
    private Score score;    // Student has a Score (포함 관계)

    public Student() {}

    public int getNo() {
        return no;
    }
    public void setNo(int no) {
        this.no = no;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Score getScore() {
        return score;
    }
    public void setScore(Score score) {
        this.score = score;
    }

}​

 

public class StudentApp {
    public static void main(String[] args) {
    
        // 1. 학생 정보를 저장하는 Student 객체 생성
        // 2. 그 학생의 성적 정보를 담는 Score 객체 생성
        // 3. 1번에서 생성된 Student객체에 성적 정보를 담고 있는 Score객체 전달(Setter 사용)
    
        Student s1 = new Student();
        s1.setNo(10);
        s1.setName("홍길동");

        Score s2 = new Score();
        s2.setKor(100);
        s2.setEng(70);
        s2.setMath(70);

        // Student 객체의 score필드에 Score객체 연결
        s1.setScore(s2);
    
        // 값 출력해보기

        int 번호 = s1.getNo();
        String 이름 = s1.getName();
        /*
        Score 점수 = s1.getScore();
        int 국어 = 점수.getKor();
        int 영어 = 점수.getEng();
        int 수학 = 점수.getMath();
        */

        int 국어 = s1.getScore().getKor();    // 위와 같은 코드임
        int 영어 = s1.getScore().getEng();
        int 수학 = s1.getScore().getMath();

        System.out.println(번호);
        System.out.println(이름);
        System.out.println(국어);
        System.out.println(영어);
        System.out.println(수학);

    }
}​

 

public abstract class Printer{

    public void on() {
        System.out.println("전원 켜기");
    }

    public void off() {
        System.out.println("전원 끄기");
    }

    public void feed() {
        System.out.println("용지 공급하기");
    }
    
    // 추상 메소드 정의
    // 모든 프린터들이 가지고 있는 출력기능(프린터마다 구현 내용이 다를 것으로 예상)을
    // void print()라는 추상 메소드로 추상화함으로써
    // 모든 프린터들이 출력 기능은 void print() {구체적인 구현내용}로 통일되게 정의하게 만듦
    public abstract void print();
}
​

 

public class ColorPrinter extends Printer {
    // Printer 클래스에 정의된 추상 메소드 재정의
    public void print() {
        System.out.println("컬러로 출력합니다.");
    }
    // ColorPrinter의 고유기능
    public void photo() {
        System.out.println("고품질 사진을 출력합니다.");
    
    }
}​

 

public class BlackAndWhitePrinter extends Printer {

    // Printer로부터 상속받은 추상메소드를 구체적인 구현 내용을 가진 메소드로 만든다.
    public void print() {
        System.out.println("흑백으로 출력합니다.");
    }

}
​

 

public class PrinterApp {
    public static void main(String[] args) {
        // 추상 클래스는 new 키워드를 사용해서 객체 생성 할 수 없다.
        // Printer p1 = new Printer();    <--에러

        BlackAndWhitePrinter p2 = new BlackAndWhitePrinter();
        p2.on();
        p2.feed();
        p2.print();
        p2.off();

        Printer p3 = new BlackAndWhitePrinter();
        p3.on();
        p3.feed();
        p3.print();
        p3.off();

        ColorPrinter p4 = new ColorPrinter();
        p4.on();
        p4.feed();
        p4.print();
        p4.off();

        Printer p5 = new ColorPrinter();
        p5.on();
        p5.feed();
        p5.print();
        p5.off();

    }
}​

 

public abstract class DatabaseAccess {
    public void connect() {
        System.out.println("데이터베이스와 연결");
    }
    public void send() {
        System.out.println("데이터베이스에 쿼리문 전송");
    }

    public void receive() {
        System.out.println("데이터베이스로부터 데이터 획득");
    }

    public void disconnect() {
        System.out.println("데이터베이스와 연결 해제");
    }
    
    public abstract void display();

    public void access() {
        connect();
        send();
        receive();
        disconnect();
        display();
    }
}
​

 

public class 성적조회 extends DatabaseAccess {
    public void display() {
        System.out.println("조회된 정보를 성적표에 표시합니다.");
    }
}
​

 

public class 출석부조회 extends DatabaseAccess {
    public void display() {
        System.out.println("조회된 정보를 출석부에 표시합니다.");
    }
}
​

 

public class DatabaseAccessApp {
    public static void main(String[] args) {
        출석부조회 a = new 출석부조회();
        a.access();
    }
}
​

 

public interface MemberService {

    // 추상 메소드
    public abstract void removeAllMemebers();

    //public abstract는 생략가능하다.
    public abstract void printAllMembers();

    public abstract void removeMemberByNo(int no);

    public abstract String getMemberNameByNo(int no);
}
​

 

public class MemberServiceImpl implements MemberService {

    public void removeAllMemebers() {
        System.out.println("엑셀파일에서 모든 회원 정보를 삭제한다.");
    }

    public void printAllMembers() {
        System.out.println("엑셀파일의 모든 회원정보를 출력한다.");
    }

    public void removeMemberByNo(int no) {
        System.out.println("엑셀파일에서 " + no + "번 회원을 삭제한다.");
    }

    public String getMemberNameByNo(int no) {
        String name = null;
        name = "홍길동";
        System.out.println("엑셀파일에서 " + no + "번 회원의 이름을 조회한다.");
        return name;
    }
}​

 

public class MemberApp {
    public static void main(String[] args) {
        //MemberService 인터페이스를 구현한 객체 생성
        MemberServiceImpl service1 = new MemberServiceImpl();
        service1.printAllMembers();

        // 인터페이스를 구현한 객체는 인터페이스 타입의 변수에 담을 수 있다.
        MemberService service2 = new MemberServiceImpl();
    }
}​

'자바 > oop5' 카테고리의 다른 글

Car, HRMgr, Mall.java  (0) 2019.06.07

+ Recent posts