九、注解、有助于更好的理解框架

注解


类:要给一个类增强一些功能?继承、实现一个接口,还可以使用注解

Class A extends B

Class A implements C

可以通过使用注解 增强类、方法、属性的功能

内置注解

1.Override

class Father{
    public void eat(){
        System.out.println("Father eat");
    }
}

class Son extends Father{
   @Override//内置注解  提示重写方法单词和父类不一样
    public void eat() {
        System.out.println("Son eat...");
    }
}




2.Deprecated压制警告

@Deprecated //给方法由于安全、性能问题等不推荐使用此方法,使用的时候有下划线
//此外,在版本升级时,如果要计划删除一次额方法,
//也通常会在前一个版本中将该方法加上
//@Deprecated,然后在后续版本中删除

class Test{
    @Deprecated
    public void foot() {
        System.out.println("测试");
    }
}

public class Demo01 {
    public static void main(String[] args) {
            Test test = new Test();
            test.foot();
    }
}

3 @SuppressWarnings 压制警告,

@SuppressWarnings   //压制警告,(不建议使用)

@SuppressWarnings(value = "unchecked")

//value的值都有哪些  unchecked、depreation(不提示过期信息)、unused(检查是否未被使用)、
                 //fallthrough(swtich是否一直往下执行,而没有breakc)
								 //path(忽略对类路径不存在的检查)
								 //serialversionUID(忽略 一个类可以序列化、但却没有序列化的 警告)
								 //all(忽略以上所有情况)
public class Demo01 {
    public static void main(String[] args) {
        List list = new ArrayList();
        //黄色波浪线,泛型检查
    }
}

自定义注解

package annotaion;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//修饰该注解能在类、属性和方法上使用
@Target(value = {ElementType.FIELD,ElementType.METHOD,
                 ElementType.TYPE,ElementType.PARAMETER,
                 ElementType.LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)

public @interface MyAnnotation {
        String value() default "张三";
        int age() default 22;
        //用定义方法的形式,定义一个属性
        //方法的名字,就是属性的名字;方法的返回值,就是属性的类型
}

-------------------------------------
  使用
package annotaion;

import jdk.internal.org.objectweb.asm.tree.analysis.Value;

import java.lang.annotation.Annotation;

public class TestMyAnnotation {

    @MyAnnotation(value = "李四", age = 24)
    public static void test() throws Exception {
        //使用注解
        Annotation[] annotations = Class.forName("annotaion.TestMyAnnotation").getMethod("test").getAnnotations();
        for (Annotation a : annotations) {
            if(a instanceof MyAnnotation){//判断注解
                System.out.println(((MyAnnotation) a).age());
                System.out.println(((MyAnnotation) a).value());
            }else{
                System.out.println(" @Deprecated...");
            }
        }
    }

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


//24
//李四
//@Deprecated...


元注解

元数据:修饰数据的数据

元注解:修饰注解的注解

自定义注解如何使用?结合反射使用

注解+反射 什么时候会真正的使用?开发框架时 Spring mybatis SpringMVC


@Target:限制注解 可以使用的位置

限制注解 能够使用哪些元素上(属性、方法、类);如果一个注解没有@Target描述,则该注解可以修饰任何类型的元素,如果有@Target修饰,该注解就只能用于被@Target修饰的地方

哪些位置 ? ElementType枚举

public enum ElementType {
    /** Class, interface (including annotation type), or enum declaration */
    TYPE,
    /** Field declaration (includes enum constants) */
    FIELD,(属性)
    /** Method declaration */
    METHOD,(方法)
    /** Formal parameter declaration */
    PARAMETER,
    /** Constructor declaration */
    CONSTRUCTOR,
    /** Local variable declaration */
    LOCAL_VARIABLE,
    /** Annotation type declaration */
    ANNOTATION_TYPE,
    /** Package declaration */
    PACKAGE,
    /**
     * Type parameter declaration
     *
     * @since 1.8
     */
    TYPE_PARAMETER,
    /**
     * Use of a type
     *
     * @since 1.8
     */
    TYPE_USE
}
@Retention:限制注解的生命周期
public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     * jvm直接将该注解丢弃
     */
    SOURCE,()

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     * .java->.class
     * 程序在编译时,会使用注解,在运行时不会使用
     */
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     * 程序在编译时及运行时,都会注解
     */
    RUNTIME
}
@Document

javaDoc:java帮助文档。.java->帮助文档

默认情况下,javadoc不包含注解的解释

如果想在javadoc文档中中也包含注解的使用说明,就需要使用@Document(放到自定义注解前)

例如,以下MyAnnotation注解 ,会在生成javadoc时,被显示在文档中

@Documented
public @inteface MyAnnotation{
  
}
@Inherited:继承
@Inherited
public @inteface MyAnnotation{
  
}
public class A{}

public class B extends A{}//默认情况下,B不会继承A 中的注解,如果继承则加继承(在自定义注解前加)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵同学‍

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值