一 Class是什么复习
Class是java类的说明书,通过反射或者hvm阅读该说明书,创建类的示例
二 说明书 annotation
2-1 定义
注解就是说明书中的一小段文本。可以携带参数,可以被扫描阅读。 只提供一小段信息,怎么处理被注解标注的东西,不是注解来管的
2-2 创建一个注解
2.2.1 代码
public @interface SpringBootApplication {}// 使用@SpringBootApplicationpublic class Node {Object value;Node next;public Node(Object value, Node next) {this.value = value;this.next = next;}public static void main(String[] args) {Node head = new Node(1, null);Node second = new Node(2, null);Node third = new Node(3, null);head.next = second;second.next = third;}}
2.2.2 元注解 标注在注解上面的注解
import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;// 什么地方可以使用这个注解@Target({ElementType.TYPE, ElementType.CONSTRUCTOR})// 声明这个注解会不会被编译器丢弃@Retention(RetentionPolicy.CLASS)public @interface SpringBootApplication {}// 还有一些其他注解,不很重要
2.2.3 注解属性
import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;// 什么地方可以使用这个注解@Target({ElementType.TYPE, ElementType.CONSTRUCTOR})// 声明这个注解会不会被编译器丢弃@Retention(RetentionPolicy.CLASS)public @interface SpringBootApplication {// 这里可以声明注解的属性String value() default "aaa";String[] description() default "aaa";}
@SpringBootApplication(value = "ccc", description = {"aaa", "bbb"})public class Node {Object value;Node next;public Node(Object value, Node next) {this.value = value;this.next = next;}public static void main(String[] args) {Node head = new Node(1, null);Node second = new Node(2, null);Node third = new Node(3, null);head.next = second;second.next = third;}}
