1일차과제-Annotaion

  1. 어노테이션을 쓰는 이유

어노테이션이란? 프로그램에게 특정한 기능을 담당한다고 명시해주는 것

  • 코드 간결성: 반복적인 코드를 줄여주어 개발자가 실제 로직에 더 집중할 수 있게 도와줌

  • 오류 감소: 컴파일 시간에 문제를 발견할 수 있어 런타임 에러의 가능성을 줄임.

  • 문서화: 어노테이션 자체가 문서의 역할을 할 수 있어, 코드의 의도를 명확하게 전달할 수 있음.

     

     

     

    2. 커스텀 어노테이션 만드는 방법.

     


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
    String description() default "No description";
    int value() default 0;
}

// 어노테이션 사용 예
public class TestAnnotation {
    @MyAnnotation(description = "This is a test method", value = 10)
    public void test() {
        System.out.println("Test method executed");
    }
}

 

댓글을 작성해보세요.