注解

注解

一、注解入门

Annotation 是 jdk1.5 开始引入的新技术。

Annotation的作用:

  1. 不是程序本身,可以对程序作出解释;

  2. 可以被其他程序(例如编译器)读取。

Annotation的格式

“@注解名”,也可以带参数,例如:

@SuppressWarnings(value=“unchcked”)

Annotation在哪里使用?

可以附加在 package、class、method、field 上,相当于给它们添加了额外的辅助信息。还可以通过反射机制编程实现对这些元数据的访问。

package com.gcbeen.annotation;

/**
 * 什么是注解?
 */
public class Test01 extends Object{
    // Override 重写的注解
    @Override
    public String toString(){
        return super.toString();
    }
}

二、内置注解

  1. @ Override:定义在 java. lang Override中,此注释只适用于修饰方法,表示一个方法声明打算重写超类中的另一个方法声明。

  2. @ Deprecated:定义在 Java. lang. Deprecated中,此注释可以用于修饰方法,属性,类,表示不鼓励程序员使用这样的元素,通常是因为它很危险或者存在更好的选择。

  3. @ SuppressWarnings:定义在 Java. lang. SuppressWarnings中,用来抑制编译时的警告信息。

@ SuppressWarnings 需要添加一个参数才能正确使用,这些参数都是已经定义好了的,我们选择性的使用就好了。

package com.gcbeen.annotation;

public class Test02 {

    @SuppressWarnings("all")
    public void testFunction01() {
        System.out.println("testFunction01");
    }

    @SuppressWarnings("unchecked")
    public void testFunction02() {
        System.out.println("testFunction02");
    }

    @SuppressWarnings(value = {"unckecked", "deprecation"})
    public void testFunction03() {
        System.out.println("testFunction03");
    }

    public static void main(String[] args) {
        System.out.println("test2");
    }

}

内置注解例子

package com.gcbeen.annotation;

import java.util.ArrayList;
import java.util.List;

public class Test03 {

    // Override 重写的注解
    @Override
    public String toString() {
        return super.toString();
    }

    // @Deprecated 不推荐使用,但可以使用,或者存在更好的更新方式
    @Deprecated
    public static void test() {
        System.out.println("Deprecated");
    }

    // @SuppressWarnings 镇压警告
    @SuppressWarnings("all")
    public void test01() {
        List<String> list = new ArrayList<>();
    }

    public static void main(String[] args) {
        test();
    }


}

三、元注解

元注解的作用就是负责注解其他注解,Java 定义了4个标准的 meta- annotation 类型,他们被用来提供对其他 annotation 类型作说明。

4个标准的 meta- annotation 类型 是: @Target,@Retention,@Documented, @Inherited。在 java. lang annotation 包中可以找到。

  1. @ Target:用于描述注解的使用范围(即被描述的注解可以用在什么地方)。

  2. @ Retention:表示需要在什么级别保存该注释信息,用于描述注解的生命周期。

源代码 SOURCE < 字节码 CLASS < RUNTIME

  1. @ Document:说明该注解将被包含在 Javadoc 中。

  2. @ Inherited:说明子类可以继承父类中的该注解。

package com.gcbeen.annotation;

import java.lang.annotation.*;

// 测试元注解
public class TestAnnotation {

    @MyAnnotation
    public void test() {
        System.out.println("TestAnnotation");
    }

    public static void main(String[] args) {
        new TestAnnotation().test();
    }

}

// 定义一个注解
/* @Target
   注解可以用在什么地方
   ElementType.METHOD 方法上有效
   ElementType.TYPE类上有效
 */
@Target(value = ElementType.METHOD)

/* @Retention
   在什么地方有效
   运行时 RUNTIME > 字节码 CLASS > 源代码 SOURCES
 */
@Retention(value = RetentionPolicy.RUNTIME)

// @Documented 表示是否将我们的注解生成在Javadoc中
@Documented

// @Inherited 子类可以继承父类的注解
@Inherited

@interface MyAnnotation {

}

四、自定义注解

使用 @interface 自定义注解时,自动继承了 java. lang annotation. Annotation 接口。

@ interface用来声明一个注解,格式:

// public @interface 注解名 { 
//     定义内容 
// }

public @interface MyAnnotation {
    // 定义参数
}

注解的参数定义格式

// 参数类型 + 参数名()
String name() default "";
int age() default 0;
int id() default -1; // -1代表不存在
String[] schools() default {"暑假好长", "不想实习"};

注解参数的定义格式很像方法的定义,对比方法的定义:

  • 方法的名称就是参数的名称。
  • 返回值类型就是参数的类型(返回值只能是基本类型, Class, String, enum)
  • 如果只有一个参数成员,一般参数名为 value ;
  • 注解的参数必须要有值,我们定义注解的参数时,经常使用空字符串,0作为默认值
  • 可以通过 default 来声明参数的默认值;
package com.gcbeen.annotation;

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

// 自定义注解
public class TestCustomAnnotation {

    // 注解可以显示赋值,如果没有默认值,就必须给注解赋值
    @MyAnnotation2(name = "王五")
    public void test() {

    }

    @MyAnnotion3("gcbeen")
    public void test2() {

    }

    public static void main(String[] args) {
        TestCustomAnnotation testCustomAnnotation = new TestCustomAnnotation();
        testCustomAnnotation.test();
        testCustomAnnotation.test2();
    }

}


@Target(value = ElementType.METHOD)
@Retention(value = RetentionPolicy.RUNTIME)
@interface MyAnnotation2 {
    // 注解的参数定义格式: 参数类型 + 参数名()
    // String name();
    String name() default "";

    int age() default 0;

    int id() default -1; // -1代表不存在

    String[] schools() default {"暑假好长", "不想实习"};
}

@Target(value = ElementType.METHOD)
@Retention(value = RetentionPolicy.RUNTIME)
@interface MyAnnotion3 {
    String value();
}
posted @ 2022-09-30 10:33  gcbeen  阅读(25)  评论(0)    收藏  举报