使用元數(shù)據(jù)可以為代碼增加一些額外的信息。元數(shù)據(jù)注解以@
開頭袁勺,其后緊跟一個(gè)編譯時(shí)常量(比如 deprecated
)或者調(diào)用一個(gè)常量構(gòu)造函數(shù)。
Dart 中有兩個(gè)注解是所有代碼都可以使用的:@deprecated
和 @override
。你可以查閱擴(kuò)展一個(gè)類獲取有關(guān) @override
的使用示例腌紧。下面是使用 @deprecated
的示例:
class Television {
/// _棄用: 使用 [turnOn] 替代_
@deprecated
void activate() {
turnOn();
}
/// 打開 TV 的電源。
void turnOn() {...}
}
可以自定義元數(shù)據(jù)注解畜隶。下面的示例定義了一個(gè)帶有兩個(gè)參數(shù)的 @todo
注解:
library todo;
class Todo {
final String who;
final String what;
const Todo(this.who, this.what);
}
使用 @todo 注解的示例:
import 'todo.dart';
@Todo('seth', 'make this do something')
void doSomething() {
print('do something');
}
元數(shù)據(jù)可以在 library寄啼、class、typedef代箭、type parameter墩划、constructor、factory嗡综、function乙帮、field、parameter 或者 variable 聲明之前使用极景,也可以在 import 或 export 之前使用察净〖蒈睿可使用反射在運(yùn)行時(shí)獲取元數(shù)據(jù)信息。