1. 개요
주석은 메소드에서 정의 될 수 있으며, 주석은 클래스와 동일합니다.
일반적으로 사용되는 주석 :
@override : 클래스를 재발하는 메소드를 의미합니다.
또한 부모 클래스 메소드가 추적되는지 여부를 결정할 수 있습니다. 덮어 쓰는 부모 클래스의 방법.
@SuppressWarnings ( "감가 상각") : 컴파일러 경고를 취소합니다 (예 : 사용중인 방법은 구식입니다)
@DepRecated :이 문장은 메소드의 맨 위에 배치 되어이 메소드가 구식이거나 클래스에 사용되었음을 나타냅니다.
코드 사본은 다음과 같습니다.
java.util.arraylist 가져 오기;
Java.util.list 가져 오기;
공개 클래스 주석장 {
/*
* 컬렉션의 경우 스토리지 유형이 지정되지 않으면 보안 경고가 있습니다.
* 보안 경고를 프롬프트하지 않으려면 @SuppressWarnings (매개 변수)를 클래스 또는 메소드에 추가하십시오.
*/
@suppresswarnings ( "확인되지 않은")
public static void main (String [] args) {
목록 목록 = new ArrayList ();
}
}
2. 사용자 정의 주석
1. 형식
권한 @interface 주석 이름 {}
단계:
주석 클래스 정의 ---> 응용 프로그램 주석 클래스의 클래스 정의 ---> 응용 프로그램 주석 클래스의 클래스를 반영하는 클래스 (이 클래스는 응용 프로그램 주석 클래스에서 별도로 정의하거나 테스트 할 수 있음)
코드 사본은 다음과 같습니다.
java.lang.annotation.trention import;
importjava.lang.annotation.retentionpolicy;
//이 주석을 바이트 코드로 유지하도록 정의하십시오
@retention (retentionpolicy.runtime)
public @interface myAnnotation {
}
@MyAnnotation
// 응용 프로그램 정의 주석 클래스
공개 클래스 ApplyAnnotation {
public static void main (String [] args) {
if (applyMyAnnotation.class.isAnnotationPresent (myAnnotation.class)) {//이 클래스에 지정된 주석 클래스가 있는지 여부를 결정합니다.
myAnnotation annotation = (myAnnotation) applyMyAnnotation.class
.getAnnotation (myAnnotation.class);
System.out.println (주석);
}
}
}
2. 선언주기
형식 : 예 : @retention (rendentionpolicy.class)
사용자 정의 주석 클래스에서 사이클 정의 @retention (매개 변수 유형) 매개 변수 유형은 rendentionpolicy입니다.
rendentionpolicy.class : 클래스 파일에서 가상 머신은 런타임 중에 주석을 유지하지 않습니다.
retentionpolicy.runtime : 클래스 파일에서 런타임 중에 가상 주석이 유지됩니다.
rendentpolicy.source : 소스 파일에서 주석을 버립니다
suppresswarnings 및 오버라이드
감가 상승은 런타임 통화로 정의 되려면 rendentpolicy.runtime이어야합니다.
기본값은 retentionpolicy.class입니다.
3. 대상을 지정합니다
형식 : 예를 들어 : Method @Target (elementType.Method)
정의 된 주석으로 구성원을 주석을 달 수 있습니다. 이 주석이 선언되지 않으면 모든 프로그램 요소에 배치 할 수 있습니다.
패키지, 인터페이스, 매개 변수, 메소드, 로컬 변수, 필드 등이 될 수 있습니다.
코드 사본은 다음과 같습니다.
//이 주석을 바이트 코드로 유지하도록 정의하십시오
@retention (retentionpolicy.runtime)
@TARGET ({ElementType.Method, ElementType.Type}) // 유형을 나타내는 메소드 및 클래스에서 정의 할 수 있습니다.
public @interface myAnnotation {
}
@MyAnnotation
// 응용 프로그램 정의 주석 클래스
공개 클래스 ApplyAnnotation {
@myAnnotation // 메소드에 정의되었습니다
public static void main (String [] args) {
if (applyMyAnnotation.class.isAnnotationPresent (myAnnotation.class)) {//이 클래스에 지정된 주석 클래스가 있는지 여부를 결정합니다.
myAnnotation annotation = (myAnnotation) applyMyAnnotation.class
.getAnnotation (myAnnotation.class);
System.out.println (주석);
}
}
}
3. 주석에 속성을 추가하십시오
1. 유형
주석의 속성 설정은 다음과 같습니다. 8 기본 데이터 유형, 문자열, 열거, 주석, 클래스, 배열 유형,
2.주의를 기울이십시오
주석에 속성이 하나만 있거나 하나의 속성 만 할당 해야하는 경우 속성 이름을 지정하지 않고 호출 할 때 직접 작성할 수 있습니다.
주석이 붙은 속성이 배열 유형이고 할당 할 때 하나의 값만 할당하면 {}를 생략 할 수 있습니다.
3. 예
3.1. attribute 유형 (IS String)
코드 사본은 다음과 같습니다.
import java.lang.annotation.elementtype;
java.lang.annotation.trention import;
java.lang.annotation.retentionpolicy import;
Java.lang.annotation import.*;
//이 주석을 바이트 코드로 유지하도록 정의하십시오
@retention (retentionpolicy.runtime)
public @interface myAnnotation {
문자열 값 ();
String Color () 기본 "Red"; // 기본값 설정은 "빨간색"입니다.
}
@MyAnnotation ( "Java")
공개 클래스 ApplyAnnotation {
public static void main (String [] args) {
/**
* 이것은 수업에서 주석을 얻는 것이며, 방법에 대한 주석을 얻을 수도 있습니다.
*/
if (applyMyAnnotation.class.isAnnotationPresent (myAnnotation.class)) {//이 클래스에 지정된 주석 클래스가 있는지 여부를 결정합니다.
myAnnotation annotation = (myAnnotation) applyMyAnnotation.class
.getAnnotation (myAnnotation.class);
System.out.println ( "value ="+annotation.value ());
System.out.println ( "color ="+annotation.color ());
}
}
}
결과:
값 = Java
색상 = 빨간색
호출 프로그램에서 하나의 속성 만 할당 할 수 있다면 속성 이름을 생략 할 수 있음을 알 수 있습니다. 그렇지 않으면 @ 주석 클래스 (속성 이름 = value)
3.2
코드 사본은 다음과 같습니다.
/*열거 클래스*/
공개 열거 주 {
태양, 월;
}
/**
* 주석 수업
*/
public @Interface AnnotationText {
문자열 값 ();
}
import java.lang.annotation.elementtype;
java.lang.annotation.trention import;
java.lang.annotation.retentionpolicy import;
Java.lang.annotation import.*;
//이 주석을 바이트 코드로 유지하도록 정의하십시오
@retention (retentionpolicy.runtime)
public @interface myAnnotation {
문자열 값 ();
String Color () 기본 "Red"; // 기본값 설정은 "빨간색"입니다.
Week Week () 기본 주간 .mon; // enum 유형
int [] array () default {1,2,3}; // 배열 유형
AnnotationText Annotation () default @annotationText ( "my");
classdemo () default integer.class; // 클래스 유형
}
@MyAnnotation (value = "java", color = "green", Week = Week.sun, array = 5, annotation =@annotationText ( "You"), classDemo = String.class) // array = {4,5, 6}
공개 클래스 ApplyAnnotation {
public static void main (String [] args) {
/**
* 이것은 수업에서 주석을 얻는 것이며, 방법에 대한 주석을 얻을 수도 있습니다.
*/
if (applyMyAnnotation.class.isAnnotationPresent (myAnnotation.class)) {//이 클래스에 지정된 주석 클래스가 있는지 여부를 결정합니다.
myAnnotation annotation = (myAnnotation) applyMyAnnotation.class
.getAnnotation (myAnnotation.class);
System.out.println ( "value ="+annotation.value ());
System.out.println ( "color ="+annotation.color ());
System.out.println ( "Week ="+annotation.week ());
System.out.println ( "배열 길이 ="+annotation.array (). 길이);
System.out.println ( "주석 유형 값 ="+annotation.annotation () value ());
System.out.println ( "클래스 유형 값 ="+annotation.classdemo ());
}
}
}
결과:
코드 사본은 다음과 같습니다.
값 = Java
색상 = 녹색
주 = 태양
배열 길이 = 1
주석 유형 값 = 귀하
클래스 유형 값 = classjava.lang.string
4. 방법에 대한 주석
코드 사본은 다음과 같습니다.
importjava.lang.annotation.- 보장;
importjava.lang.annotation.retentionpolicy;
/**
*주석 수업
*/
@retention (retentionpolicy.runtime)
public @Interface AnnotationText {
StringValue ();
}
publicclassApplymyAnnotation {
publicstaticVoidMain (String [] args) ThrowSexection {
MethodMethodShow = applyMyAnnotation.class.getMethod ( "show");
AnnotationTextAnno = MethodShow.getAnnotation (AnnotationText.class);
System.out.println (Anno.Value ());
}
@annotationText ( "Java")
publicvoidshow () {
System.out.println ( "Hello");
}
}
결과 : Java