Java 에서 Unsupported Operation 예외 이해

1710 단어 ThinkinginJava
자바 Collection 인터페이스 에서 각종 추가 또는 제거 방법 을 실행 하 는 것 은 선택 할 수 있 는 작업 입 니 다. 즉, 클래스 를 실현 하 는 것 은 이러한 방법 에 기능 정 의 를 제공 할 필요 가 없습니다.실현 방법 은 추상 류 에서 이러한 선택 가능 한 방법 을 정의 하고 방법론 에서 Unsupported Operation Exception 이상 을 던 지 는 것 이다.예 를 들 어 AbstractList 의 일부 동작:
/**
     * {@inheritDoc}
     *
     * 

This implementation always throws an * {@code UnsupportedOperationException}. * * @throws UnsupportedOperationException {@inheritDoc} * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} * @throws IndexOutOfBoundsException {@inheritDoc} */ public E set(int index, E element) { throw new UnsupportedOperationException(); } /** * {@inheritDoc} * *

This implementation always throws an * {@code UnsupportedOperationException}. * * @throws UnsupportedOperationException {@inheritDoc} * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} * @throws IndexOutOfBoundsException {@inheritDoc} */ public void add(int index, E element) { throw new UnsupportedOperationException(); } /** * {@inheritDoc} * *

This implementation always throws an * {@code UnsupportedOperationException}. * * @throws UnsupportedOperationException {@inheritDoc} * @throws IndexOutOfBoundsException {@inheritDoc} */ public E remove(int index) { throw new UnsupportedOperationException(); }

좋은 웹페이지 즐겨찾기