코드 그라데이션

후발대 11일차 전체 내용 본문

Java/후발대

후발대 11일차 전체 내용

완벽한 장면 2023. 1. 23. 16:51

추상클래스, 인터페이스 실습

 public abstract class Camera {

    public void takePicture(){
        System.out.println("사진을 촬영합니다.");
    }

    public void recordVideo(){
        System.out.println("동영상을 녹화합니다.");
    }

    public abstract void showMainFeature();
}


----
public class AbstractTest {
    public static void main(String[] args) {
//        Camera camera = new Camera();
}
}
------------
public class FactoryCam extends Camera{

@Override
    public void showMainFeature() {
        System.out.println("화재감지");
    }


-----------
public class SpeedCam extends Camera{

    @Override
    public void showMainFeature() {
        System.out.println("속도측정, 번호인식");
    }


}


------------
public class AbstractTest {
    public static void main(String[] args) {
//        Camera camera = new Camera();
        FactoryCam factoryCam = new FactoryCam();
        factoryCam.showMainFeature();

        SpeedCam speedCam = new SpeedCam();
        speedCam.showMainFeature();
    }
}

--------------
// 실습 2 . reporter 인터페이스

public class InterTest {
    public static void main(String[] args) {

}
}

------
public class FactoryCam extends Camera{

@Override
    public void showMainFeature() {
        System.out.println("화재감지");
    }


    public void report(){
        System.out.println("화재를 신고합니다.");
    }

    public void detect() {
        System.out.println("화재를 감지합니다.");
  }
}
--------

public class SpeedCam extends Camera{

    @Override
    public void showMainFeature() {
        System.out.println("속도측정, 번호인식");
    }

    public void report(){
        System.out.println("사고 신고를 진행합니다.");
    }
    public void detect() {
        System.out.println("사고를 감지합니다.");
    }
}

-------------------

// reporter pkg

public interface Reportable {
    void report();
}

-------------
public class NormalReporter implements Reportable{
    @Override
    public void report() {
        System.out.println("일반 화재 신고를 합니다.");
    }
}

---------------
public class AdvancedReporter implements Reportable{

    @Override
    public void report() {
        System.out.println("직전 30초 영상과 함께 신고를 진행합니다.");
    }
}
---------------------

public class InterTest {
    public static void main(String[] args) {

        Reportable normalReporter = new NormalReporter();
        Reportable advancedReporter = new AdvancedReporter();

        normalReporter.report();
        advancedReporter.report();

}
}

----------------------

// 실습 3. dectector pkg


public interface Detectable {
    public static final String name = "감지자";
    public abstract void detect();
}

-----
public class FireDetector implements Detectable{
    @Override
    public void detect() {
        System.out.println("일반성능으로 화재를 감지합니다.");
    }
}

-----

public class AdvancedFireDetector implements Detectable{
    @Override
    public void detect() {
        System.out.println("향상된 성능으로 화재를 감지합니다.");
    }
}

-----------------
public class InterTest {
    public static void main(String[] args) {
Reportable normalReporter = new NormalReporter();
        Reportable advancedReporter = new AdvancedReporter();

        normalReporter.report();
        advancedReporter.report();

        System.out.println("--------------------");

        Detectable fireDetector = new FireDetector();
        fireDetector.detect();
        Detectable advancedFireDetector = new AdvancedFireDetector();
        advancedFireDetector.detect();

    }
}

------

// FactoryCam 실습, 만든 기능등을 factorycam에 적용시켜보기

public class FactoryCam extends Camera implements Detectable, Reportable {

    private Detectable detector;
    private Reportable reporter;

    public void setDetector(Detectable detector) {
        this.detector = detector;
    }

    public void setReporter(Reportable reporter) {
        this.reporter = reporter;
    }

    @Override
    public void showMainFeature() {
        System.out.println("화재감지");
    }

    @Override
    public void detect() {
        detector.detect();
    }

    @Override
    public void report() {
        reporter.report();

    }

}
---------------------
public class InterTest {
    public static void main(String[] args) {

        Reportable normalReporter = new NormalReporter();
        Reportable advancedReporter = new AdvancedReporter();

        normalReporter.report();
        advancedReporter.report();

        System.out.println("--------------------");

        Detectable fireDetector = new FireDetector();
        fireDetector.detect();
        Detectable advancedFireDetector = new AdvancedFireDetector();
        advancedFireDetector.detect();

        System.out.println("-----------------------");

        FactoryCam factoryCam = new FactoryCam();
        factoryCam.setDetector(advancedFireDetector);
        factoryCam.setReporter(advancedReporter);

        factoryCam.detect();
        factoryCam.report();
    }
}
728x90
Comments