[Java]注解

------------恢复内容开始------------

内置注解:

package Comments;

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

/**
 * 注解学习
 *
 * @author ZhaoLu cang on 2021/4/8 0008
 */
public class Comments extends Object{

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

    // Deprecated 不推荐使用,或者存在更好的方法,已经过时
    @Deprecated
    public static void deprecated(){
        System.out.println("Deprecated");
    }

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

    //镇压警告,消除所有警告,可以加在类名上
    @SuppressWarnings("all")
    public static void suppresswarnings(){
        List list=new ArrayList();
    }
}

元注解:负责解释其他注解的注解

@Target:用于描述注解的解释范围

@Retention:用什么级别保存注解信息,source<class<runtime

@Document::说明该注解包含在javadoc中

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

package Comments;

import java.lang.annotation.*;

/**
 * 测试元注解
 *
 * @author ZhaoLu cang on 2021/4/8 0008
 */
@MyAnnotation
public class Comments02 {

    public void test(){

    }
}

//定义一个注解,Target表示我们的注解可以用在哪些地方
@Target(value = {ElementType.METHOD,ElementType.TYPE})
//表示注解在什么时候有效,代码source 编译后class 运行时runtime
@Retention(value = RetentionPolicy.RUNTIME)
//Documented 表示是否将注解生成在Javadoc中
@Documented
//Inherited 子类可以继承父类的注解
@Inherited
@interface MyAnnotation{

}

自定义注解

@Interface自定义注解,继承了java.lang.annoctation.Annoctation

 

posted @ 2021-04-09 12:37  雾霾王者  阅读(39)  评论(0编辑  收藏  举报