Skip to content

Commit 3615e0b

Browse files
committed
[docs update] 添加注解相关内容
1 parent 7025349 commit 3615e0b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

docs/java/basis/java基础知识总结.md

+25
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,31 @@ public class DebugInvocationHandler implements InvocationHandler {
11991199

12001200
这些都是因为你可以基于反射分析类,然后获取到类/属性/方法/方法的参数上的注解。你获取到注解之后,就可以做进一步的处理。
12011201

1202+
## 注解
1203+
1204+
`Annontation` (注解) 是Java5 开始引入的新特性,可以看作是一种特殊的注释,主要用于修饰类、方法或者变量。
1205+
1206+
注解本质是一个继承了`Annotation` 的特殊接口:
1207+
1208+
```java
1209+
@Target(ElementType.METHOD)
1210+
@Retention(RetentionPolicy.SOURCE)
1211+
public @interface Override {
1212+
1213+
}
1214+
1215+
public interface Override extends Annotation{
1216+
1217+
}
1218+
```
1219+
1220+
注解只有被解析之后才会生效,常见的解析方法有两种:
1221+
1222+
- **编译期直接扫描** :编译器在编译 Java 代码的时候扫描对应的注解并处理,比如某个方法使用`@Override` 注解,编译器在编译的时候就会检测当前的方法是否重写了父类对应的方法。
1223+
- **运行期通过反射处理** :像框架中自带的注解(比如 Spring 框架的 `@Value``@Component`)都是通过反射来进行处理的。
1224+
1225+
JDK 提供了很多内置的注解(比如 `@Override``@Deprecated`),同时,我们还可以自定义注解。
1226+
12021227
## 异常
12031228

12041229
### Java 异常类层次结构图

0 commit comments

Comments
 (0)