Skip to content

Commit 0ec9e3c

Browse files
committed
add
1 parent b10f00d commit 0ec9e3c

1 file changed

Lines changed: 69 additions & 1 deletion

File tree

Android加强/注解使用.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,58 @@ public @interface UseCase {
7373

7474
###自定义`Annotation`
7575

76+
假设现在有个开发团队在每个类的开始都要提供一些信息,例如:
77+
78+
```java
79+
public class Generation3List extends Generation2List {
80+
81+
// Author: John Doe
82+
// Date: 3/17/2002
83+
// Current revision: 6
84+
// Last modified: 4/12/2004
85+
// By: Jane Doe
86+
// Reviewers: Alice, Bill, Cindy
87+
88+
// class code goes here
89+
90+
}
91+
```
92+
93+
我们可以声明一个注解来保存这些相同的元数据。如下:
94+
```java
95+
@interface ClassPreamble {
96+
String author();
97+
String date();
98+
int currentRevision() default 1;
99+
String lastModified() default "N/A";
100+
String lastModifiedBy() default "N/A";
101+
// Note use of array
102+
String[] reviewers();
103+
}
104+
```
105+
声明完注解之后我们就可以填写一些参数来使用它,如下:
106+
107+
```java
108+
@ClassPreamble (
109+
author = "John Doe",
110+
date = "3/17/2002",
111+
currentRevision = 6,
112+
lastModified = "4/12/2004",
113+
lastModifiedBy = "Jane Doe",
114+
// Note array notation
115+
reviewers = {"Alice", "Bob", "Cindy"}
116+
)
117+
public class Generation3List extends Generation2List {
118+
119+
// class code goes here
120+
121+
}
122+
```
123+
124+
125+
126+
127+
76128

77129

78130
###`Annotation`解析
@@ -194,7 +246,23 @@ dependencies {
194246
- `@UiThread`
195247
`UI thread`调用。
196248
- `@RequiresPermission`
197-
来验证该方法的调用者所需要有的权限。检查一个列表中的任何一个权限可以使用`anyOf`属性。想要检查一个权限集合
249+
来验证该方法的调用者所需要有的权限。检查一个列表中的任何一个权限可以使用`anyOf`属性。想要检查多个权限时,可以使用`allOf`属性。如下:
250+
```java
251+
@RequiresPermission(Manifest.permission.SET_WALLPAPER)
252+
public abstract void setWallpaper(Bitmap bitmap) throws IOException;
253+
```
254+
检查多个权限:
255+
256+
```java
257+
@RequiresPermission(allOf = {
258+
Manifest.permission.READ_EXTERNAL_STORAGE,
259+
Manifest.permission.WRITE_EXTERNAL_STORAGE})
260+
public static final void copyFile(String dest, String source) {
261+
...
262+
}
263+
264+
```
265+
198266

199267

200268
例如:

0 commit comments

Comments
 (0)