注解

内置注解

@Override

当子类重写父类的方法时可以加上这个注解。

这可以确保子类确实重写了父类的方法,避免出现低级错误

也就是说这个注解具有检查的作用

public class Z4 {
    @Override
    public String toString() {
        return super.toString();
    }
}

 

public class Z4 { @Override public String tostring() { return super.toString(); } }

上述就是重写toString方法,它的检查作用就是当你将toString小写为 tostring编译器会报错

2.@Deprecated

public class Z4 { @Override public String tostring() { return super.toString(); } }

上述就是重写toString方法,它的检查作用就是当你将toString小写为 tostring编译器会报错

这个注解用于表示某个程序元素类,方法等已过时,当其他程序使用已过时的类,方法时编译器会给出警告(删除线,这个见了不少了吧)。

3.@SuppressWarnings

镇压警告

被该注解修饰的元素以及该元素的所有子元素取消显示编译器警告,例如修饰一个类,那他的字段,方法都是显示警告

自定义注解和元注解

在定义注解

这个注解用于表示某个程序元素类,方法等已过时,当其他程序使用已过时的类,方法时编译器会给出警告(删除线,这个见了不少了吧)。

3.@SuppressWarnings

镇压警告

被该注解修饰的元素以及该元素的所有子元素取消显示编译器警告,例如修饰一个类,那他的字段,方法都是显示警告

自定义注解和元注解

在定义注解

public @interface +名字{}

public @interface MyAnnotation{

}

元注解

什么是元注解?

套娃 修饰注解的

@Target

//注解的作用范围

@Retention

 

//表示在什么地方还有生效

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

//服务端
public class Z3 {
    @MyAnnotation(schools = 1)  //如果没有默认值 就必须写参数
    public static void main(String[] args) {
    }

    //定义一个注解
    //注解的作用范围
    @Target({ElementType.METHOD,ElementType.TYPE})
    //表示在什么地方还有生效
    @Retention( RetentionPolicy.RUNTIME)
    @interface MyAnnotation{
        //注解的参数  参数的类型 + 参数的名字()
        String name() default "" ;
        int id() default  0;

        int[] schools();

    }
}

2021-03-29 20:42:17

posted @ 2021-03-29 20:42  域明夜  阅读(52)  评论(0)    收藏  举报