使用元数据给你的代码添加其他额外信息。 元数据注解是以 @ 字符开头,后面是一个编译时 常量(例如 deprecated)或者 调用一个常量构造函数。
有三个注解所有的 Dart 代码都可以使用: @deprecated、 @override、 和 @proxy。关于 @override 和 @proxy 示例请参考 Extending a class。 下面是使用 @deprecated 的 示例:
class Television { /// _Deprecated: Use [turnOn] instead._ @deprecated void activate() { turnOn(); } /// Turns the TV's power on. void turnOn() { print('on!'); } }
你还可以定义自己的元数据注解。 下面的示例定义了一个带有两个参数的 @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'); }
元数据可以在 library、 class、 typedef、 type parameter、 constructor、 factory、 function、 field、 parameter、或者 variable 声明之前使用,也可以在 import 或者 export 指令之前使用。 使用反射可以在运行时获取元数据 信息。
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did129481