inblog logo
|
programmer
    Java

    인터페이스

    [Java] 인터페이스(Interface)와 추상 클래스(abstract class) 차이점, JFrame 간단 예시
    Dec 26, 2023
    인터페이스
    Contents
    JFrame 간단예시
    ⚡
    인터페이스는 일방적인 약속이고, 강제성이 있음 추상 클래스와 거의 유사하나 차이가 있음. 인터페이스가 먼저 만들어지고, 클래스들이 만들어짐 추상 클래스는 클래스가 먼저 만들어지고, 추상 클래스가 만들어짐
     
    interface 코드 예시
    package ex07.example; interface Remocon{ void on(); void off(); } class SamsongRemocon implements Remocon{ @Override public void on() { System.out.println("삼성 리모컨 On"); } @Override public void off() { System.out.println("삼성 리모컨 Off"); } } class LgRemocon implements Remocon{ @Override public void on() { System.out.println("엘지 리모컨 On"); } @Override public void off() { System.out.println("엘지 리모컨 Off"); } } /** * 작성자 : 홍길동 * 날짜 : 2023.12.26 * 구현체 : SaumsungRemot, LgRemocon */ class CommonRemocon{ // 기본적으로 주어진다. private Remocon r; public CommonRemocon(Remocon r) { this.r = r; } public void on(){ r.on(); } public void off(){ r.off(); } } public class InterEx01 { public static void main(String[] args) { CommonRemocon cr = new CommonRemocon(new SamsongRemocon()); cr.on(); } }
     
    이벤트
    package ex07.example; // 라이브러리 판매자가 생성 interface EventListener{ void action(); } class MyApp{ public void click(EventListener l){ l.action(); } } //-------------------------- public class InterEx02 { public static void main(String[] args) { MyApp app = new MyApp(); app.click(() -> System.out.println("회원가입 로직이 실행됩니다.")); // 이렇게만 해주면 구현체를 작성하지 않아도됨. } }

    번외

    JFrame 간단예시

    import javax.swing.*; public class MyFrameEx01 { static int num = 1; public static void main(String[] args) { JFrame jf = new JFrame(); // jf.setLayout(new BorderLayout()); jf.setSize(300,500); JButton btn1 = new JButton("더하기"); JButton btn2 = new JButton("빼기"); JLabel la1 = new JLabel((num+"")); jf.add("North",btn1); jf.add("South",btn2); jf.add("Center",la1); btn1.addActionListener(e -> { num++; la1.setText(num+""); }); btn2.addActionListener(e -> { // Ctrl + Enter 하면 자동 양식 num--; la1.setText(num+""); }); jf.setVisible(true); } }

    라이브러리 : 여러개의 패키지 패키지 : 클래스 파일들이 모여있는 폴더 Int를 Integer로 쓸 수 있다. Integer는 메서드를 사용할 수 있음.
     
    Share article

    programmer

    RSS·Powered by Inblog