인터페이스

hayden·2023년 8월 26일

인터페이스 작성 문법

  • 인터페이스 이름은 Upper CamelCase 로작성
  • 확장자는 .java
  • 모든 필드는 public static final
  • 모든 메소드는 public abstract (java7까지)
    (final, abstract 를 생략하면 자동으로 붙는다)
  • java8 부터는 default 메소드, static 메소드 선언도 가능

인터페이스 만들어보자

new > Java Class > Interface

package com.example;

public interface LottoMachine {
    int MAX_BALL_COUNT = 45;
    int RETURN_BALL_COUNT = 6;
    public void setBalls(ball[] balls);
    public void mix();
    public ball[] getBalls();
}

⬆️ 위에선 abstract 는 생략되었다
(모든 메소드는 public abstract)

static final 도 생략되었다
(모든 필드는 public static final)

생략가능하다는 뜻

인터페이스는 껍데기다

  • 인스턴스는 생성불가하다 껍데기니까
  • todo list 라고 생각하자

implements

인터페이스를 구현하기 위해 만들자

new > Java Class > Class

package com.example;

public class LottoMachineImpl implements LottoMachine{
}

이렇게만 만들어놓으면
에러가 뜬다

인터페이스에 만들어놓은 메소드를
오버라이딩 해줘야함

option + enter
눌러주면 에러 해결사가 뜸
implement mothods
를 선택하면 알아서 오버라이딩 메소드를 찍어줌

package com.example;

public class LottoMachineImpl implements LottoMachine{
    @Override
    public void setBalls(ball[] balls) {
        
    }

    @Override
    public void mix() {

    }

    @Override
    public ball[] getBalls() {
        return new ball[0];
    }
}

실행을 위해

LottoMachineMain 을 작성한다

package com.example;

public class LottoMachineMain {
    public static void main(String[] args) {
        LottoMachine lm = new LottoMachineImpl();
    }
}

인터페이스는 인스턴스를 만들수 없으므로
위와같이 임플리먼츠 인스턴스를 생성한다
지금까지 4개의 파일을 생성했다

  • 인터페이스
  • ball
  • 임플레먼트
  • 메인

완성


// interface

package com.example;

public interface LottoMachine {
    int MAX_BALL_COUNT = 9999;
    int RETURN_BALL_COUNT = 6;
    public void setBalls(Ball[] balls);
    public void mix();
    public Ball[] getBalls();
}

// implement

package com.example;

public class LottoMachineImpl implements LottoMachine{
    private Ball[] balls;

    @Override
    public void setBalls(Ball[] balls) {
        this.balls = balls;
    }

    @Override
    public void mix() {
        for(int i=0;i<10000;i++){
            int x1=(int)(Math.random()*MAX_BALL_COUNT);
            int x2=(int)(Math.random()*MAX_BALL_COUNT);
            if(x1 != x2){
                Ball tmp = balls[x1];
                balls[x1] = balls[x2];
                balls[x2] = tmp;
            } // if
        } // for
    }

    @Override
    public Ball[] getBalls() {
        Ball[] res = new Ball[RETURN_BALL_COUNT];
        for(int i=0;i<RETURN_BALL_COUNT;i++){
            res[i] = balls[i];
        }
        return res;
    }
}

// Main

package com.example;

public class LottoMachineMain {
    public static void main(String[] args) {
        // array with 45 slots
        Ball[] balls = new Ball[LottoMachineImpl.MAX_BALL_COUNT]; // 배열을 만들어줌
        for(int i=0;i<LottoMachineImpl.MAX_BALL_COUNT;i++){
            balls[i] = new Ball(i+1); // 배열에다 값을 넣어줌
        }
        System.out.println(balls[0]);
        // create instance
        LottoMachine lm = new LottoMachineImpl();
        lm.setBalls(balls);
        lm.mix();
        lm.getBalls();
        Ball[] result = lm.getBalls();

        for(int i=0;i<result.length;i++){
            System.out.println(result[i].getNum());
        }
    }
}

// Ball

package com.example;

public class Ball {
    private int number; // 필드
    public Ball(int number){ // 생성자
        this.number = number;
    } // 필드 number 값을 설정해주는 set 역할

    // 필드는 private 이므로 외부에서 값을 받아올 수있게 public 생성자도 필요함
    public int getNum(){
        return number;
    }
}

profile
22.12.01 코딩공부기록저장소

0개의 댓글