1.注解
注解:注释和解释代码: 使用@ xxx
内置注解:
@Override: 重写父类的方法
@Deprecated :废弃 ,不推荐使用,但是可以使用。
@SuppressWarning ("all"): 忽略警告信心;
元注解:
负责解释其他注解的注解:
4种元注解:
@Target :用于描述注解的使用范围。
@Retention : 表示需要什么级别保存该注解,表示代码在什么地方还有效果。
@Document :说明该注解将被包含在javadoc,表示注解是否额可以写在文档种。
@Inherited : 说明子类可以继承父类的该注解。
自定义注解:
使用@interface Myannotation {} 来定义注解:
package com.chapter02_注解和反射.annotation;
/**
* @author Lenovo
* @date 2024/3/12
* @time 19:10
* @project Chapter01_Java_多线程
**/
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/***
* 自定义注解:
*/
public class TestAnnotation01 {
@Myannotation01(id=1)
public void test01(){
}
}
//元注解
@Target({ElementType.ANNOTATION_TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface Myannotation01{
//自定义注解参数:类型 名称+() default:是默认的;
String name() default "lilei";
int id();
int age() default 18;
}