zoukankan      html  css  js  c++  java
  • java注解

    java注解的有关知识

    一.基本的Annotation
    使用 Annotation时要在其前面增加@符号, 并把该 Annotation 当成一个修饰符使用。用于修饰它支持的程序元素
    三个基本的 Annotation:
    @Override: 限定重写父类方法, 该注释只能用于方法
    @Deprecated: 用于表示某个程序元素(类, 方法等)已过时
    @SuppressWarnings: 抑制编译器警告
    例:
    import java.util.ArrayList;
    import java.util.List;
    /*
     * 注解
     * 1.JDK提供的常用的注解
     *   @Override:限定重写父类方法,该注释只能用于方法
     *   @Deprecated:用于表示某个程序元素(类,方法等)已过时
     *   @SupressWarnings:抑制编译器警告
     * 2.如何自定义一个注释
     * 3.元注解
     * */
    public class TestAnnotation {
        public static void main(String[] args){
            Person person=new Student("tom",21);
            person.walk();
            person.eat();
            
            @SuppressWarnings({ "unused", "rawtypes" })
            List list=new ArrayList();
    
            @SuppressWarnings("unused")
            int i=10;  
        }
    }
    class Student extends Person{
        public Student(String name, int age) {
            super(name, age);
        }
        @Override
        public void walk(){
            System.out.println("学生走路");
        }
        @Override
        public void eat(){
            System.out.println("学生吃饭");
        }
    }
    @Deprecated
    class Person{
        private String name;
        private int age;
        
        public Person(String name, int age) {
            super();
            this.name = name;
            this.age = age;
        }
        
        public void walk(){
            System.out.println("走路");
        }
        @Deprecated
        public void eat(){
            System.out.println("吃饭");
        }
        @Override
        public String toString(){
            return "Person [name="+name+",age="+age+"]";
        }
    }  
    二.自定义 Annotation
    1.定义新的 Annotation 类型使用 @interface 关键字
    2.Annotation 的成员变量在 Annotation 定义中以无参数方法的形式来声明. 其方法名和返回值定义了该成员的名字和类型. 
    3.可以在定义 Annotation 的成员变量时为其指定初始值, 指定成员变量的初始值可使用 default 关键字
       public @interface MyAnnotation{
           String name() default “hello";
            }
    4.没有成员定义的 Annotation 称为标记; 包含成员变量的 Annotation 称为元数据 Annotation
    例:
    //自定义的注解
    public @interface MyAnnotation {
         String value() default "hello";
    }
    
    import java.util.ArrayList; import java.util.List; /* * 注解 * 1.JDK提供的常用的注解 * @Override:限定重写父类方法,该注释只能用于方法 * @Deprecated:用于表示某个程序元素(类,方法等)已过时 * @SupressWarnings:抑制编译器警告 * 2.如何自定义一个注释 * 3.元注解 * */ public class TestAnnotation { public static void main(String[] args){ Person person=new Student("tom",21); person.walk(); person.eat(); @SuppressWarnings({ "unused", "rawtypes" }) List list=new ArrayList(); @SuppressWarnings("unused") int i=10; } } @MyAnnotation(value="atguigu") class Student extends Person{ public Student(String name, int age) { super(name, age); } @Override public void walk(){ System.out.println("学生走路"); } @Override public void eat(){ System.out.println("学生吃饭"); } } @Deprecated class Person{ private String name; private int age; @MyAnnotation(value="atguigu") public Person(String name, int age) { super(); this.name = name; this.age = age; } @MyAnnotation(value="atguigu") public void walk(){ System.out.println("走路"); } @Deprecated public void eat(){ System.out.println("吃饭"); } @Override public String toString(){ return "Person [name="+name+",age="+age+"]"; } }
    三.元注解
    JDK 的元 Annotation 用于修饰其他 Annotation 定义
    JDK5.0提供了专门在注解上的注解类型,分别是:Retention、Target、Documented、Inherited
    @Retention:只能用于修饰一个 Annotation 定义, 用于指定该 Annotation 可以保留多长时间,必须为该成员变量RetentionPolicy 指定值:
            RetentionPolicy.SOURCE: 编译器直接丢弃这种策略的注释
            RetentionPolicy.CLASS: 编译器将把注释记录在 class 文件中. 当运行 Java 程序时, JVM 不会保留注解。 这是默认值
            RetentionPolicy.RUNTIME:编译器将把注释记录在 class 文件中. 当运行 Java 程序时, JVM 会保留注释. 程序可以通过反射获取该注释
    @Target: 用于修饰 Annotation 定义, 用于指定被修饰的 Annotation 能用于修饰哪些程序元素. @Target 也包含一个名为 value 的成员变量.
     
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    import static java.lang.annotation.ElementType.TYPE;
    import static java.lang.annotation.ElementType.FIELD;
    import static java.lang.annotation.ElementType.CONSTRUCTOR;
    import static java.lang.annotation.ElementType.METHOD;
    import static java.lang.annotation.ElementType.PARAMETER;
    import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
    //自定义的注解
    @Target({TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyAnnotation {
         String value() default "hello";
    }
     
  • 相关阅读:
    1012 The Best Rank (25 分)(排序)
    1011. World Cup Betting (20)(查找元素)
    1009 Product of Polynomials (25 分)(模拟)
    1008 Elevator (20 分)(数学问题)
    1006 Sign In and Sign Out (25 分)(查找元素)
    1005 Spell It Right (20 分)(字符串处理)
    Kafka Connect 出现ERROR Failed to flush WorkerSourceTask{id=local-file-source-0}, timed out while wait
    flume、kafka、avro组成的消息系统
    Java23种设计模式总结【转载】
    Java编程 思维导图
  • 原文地址:https://www.cnblogs.com/xujiming/p/4637545.html
Copyright © 2011-2022 走看看