Java --- Notes

1. What is a comment

effect: Not the program itself, can explain the program (and this is a comment (comment) makes no difference); may be other programs: read (such as compilers)
format: The "@ Note Name" exists in the code, can also add some parameters
such as: @SuppressWarnings (value = "unchecked" )
Where to use : Can be attached to the above package, class, method, field, etc., to give them the equivalent of adding an extra auxiliary information, we can achieve access to these metadata through programming reflection.

2. Built-in notes

3 built-in Notes:
@Override - check if the method is overloaded methods. If you find that the parent class, or to an interface and not the method, the compiler will report errors.
@Deprecated - marking outdated methods. If you use this method, the compiler will report a warning. Represents encourage programmers to use such elements, typically because it is dangerous or there is a better choice
@SuppressWarnings - instructs the compiler to ignore the warning notes in the statement. Somewhat different to the previous two comments, you need to add a parameter to the proper use of these parameters are well defined, the use of our selective.
@SuppressWarnings ( "All")
@SuppressWarnings ( "an unchecked") @SuppressWarnings (value = { "an unchecked", "deprecation"})

public class Test1{
    public void main(String[] args) {
       test();
       toString();
    }

    //标注这个方法,过时的,或者危险的,不建议使用,但是可以使用!
    @Deprecated
    public int test(){  //不建议使用
        System.out.println("aaaaaa");
        return 1;
    }

    public String hello(){ //没有被使用
       List list = new ArrayList<>();
       return "hello,world!";
    }

    @Override //标注了这个注解,就代表子类重写了父类的方法,而且必须保持一致
    public String toString() {
        return "Test1{}";
    }
}

3. yuan notes

Meta-annotation role is responsible for other notes annotations, Java defines four standard meta-annotation type, they are used to provide additional annotation types stated.
@Target , @Retention , @Documented , @Inherited
@Target : used to describe the use of annotations (ie: to be described annotations can be used in any place)
@Retention : expressed the need to save at what level the comment information, comment used to describe the life cycle of
@Document: Description this annotation will be included in the javadoc
@Inherited: Description subclass can inherit the parent class this comment

public class Test2 {

    private int age;

    @MyAnnotation
    public int getAge() {
        return age;
    }
}
//如何自定义注解呢?  @interface 注解名,注意和 interface的区别

//除了这四个注解之外的所有注解,都叫做自定义注解!

@Target(ElementType.METHOD) //表示我这个注解,能够注解谁!  方法,字段,类
@Retention(RetentionPolicy.RUNTIME) //自定义注解我们都会使用 RetentionPolicy.RUNTIME 在运行时生效
@Documented //表示L可以在Javadoc中生成信息,没什么用!
@Inherited //表示子类可以继承父类的注解,一般也不用!
@interface MyAnnotation{
}

4. custom annotation

@Interface use custom annotations automatically inherits java.lang.annotation.Annotation Interface

  • @ Interface is used to declare a comment
    format: public @ interface defined annotation content name} {
  • Each of which method is actually declares a configuration parameter.
  • The name of the method is the name of the parameter.
  • The return value type is a type parameter (return value can only be a primitive type, Class, String, enum).
  • You can declare the default value of the parameter by default
  • If only one member of parameters, general parameter called value
  • Notes element must have a value, when we define annotation elements, often use the empty string as the default value 0
public class Test3 {
//注解可以显示赋值,如果没有赋值,我们必须给出注解赋值
    @MyAnnotation2(name = "yang",age = 18 ,id = -1,schools = "123")
    public  void test(){
        
    }
    private int age;
    @MyAnnotation3("aaa")
    public int getAge() {
        return age;
    }

}

@Target(value={ElementType.METHOD}) //在类上方法上
@Retention(value = RetentionPolicy.RUNTIME)
@interface MyAnnotation2{
    String name() default "";
    int age() default 0;
    int id() default -1; // String indexOf("abc")   -1, 找不到,不存在
    String[] schools();

}

@Target(value={ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
@interface MyAnnotation3{
    String[] value(); //只有一个参数的一般名字叫做value, 可以省略!

}
Published 39 original articles · won praise 1 · views 556

Guess you like

Origin blog.csdn.net/love_to_share/article/details/103472164
Recommended