함수형 인터페이스
@FunctionalInterface: 일반적으로 구현해야 할 추상 메서드가 하나만 정의된 인터페이스를 가르킨다. 자바 컴파일러는 이렇게 명시된 함수형 인터페이스에 두 개 이상의 메서드가 선언되면 에러를 발생시킨다.
@FunctionalInterface //구현해야 할 메소드가 한개이므로 Functional Interface이다.
public interface Math {
public int Calc(int first, int second);
}
@FunctionalInterface //구현해야 할 메소드가 두개이므로 Functional Interface가 아니다. (오류 사항)
public interface Math {
public int Calc(int first, int second);
public int Calc2(int first, int second);
}
함수형 인터페이스 사용 예
함수형 인터페이스 선언
public interface PreparedStatementSetter {
void setValues(PreparedStatement pstmt) throws SQLException;
}
함수형 인터페이스 사용
PreparedStatementSetter pstmt = new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement pstmt) throws SQLException {
for (int i = 0; i < parameters.length; i++) {
pstmt.setObject(i + 1, parameters[i]);
}
}
};
'Java' 카테고리의 다른 글
[Java] 톰캣(WAS)에서의 URL 패턴과 관련된 이슈 / 자동으로 마지막 슬래시(/)가 추가되어서 리다이렉트 되는 경우 (0) | 2023.03.26 |
---|---|
[Java] SQLException에 대해서 / 무분별한 try-catch절 (0) | 2023.03.24 |
[Java] DAO 클래스의 리팩토링(inser, update 쿼리) (0) | 2023.03.22 |
[Java] MVC 프레임워크 구현 (0) | 2023.03.21 |
[Java] static 블록 / 초기화 블럭 (0) | 2023.03.21 |