多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
如果基础的可见性(第4.4.1节)选项不满足需求,可以使用访问控制。它适用于类级别和字段级别,知道两个方向: > Access control can be used if the basic visibility (4.4.1) options are not sufficient. It is applicable at class-level and at field-level and knows two directions: 允许访问:目标是一个授予的访问指定类或者字段,使用 :allow(target) 元数据(第6.9节)。 禁止访问:目标是一个禁止访问的指定类或字段,使用 :access(target) 元数据(第6.9节)。 > **Allowing access**: The target is granted access to the given class or field by using the :allow(target) metadata (6.9). > **Forcing access**: A target is forced to allow access to the given class or field by using the :access(target) metadata (6.9). 在这个上下文中,一个目标可以是一个点路径(第3.7节)到: * 一个类字段 * 一个类或者抽象类型 * 一个包 > In this context, a target can be the dot-path (3.7) to > * a class field, > * a class or abstract type, or > * a package. 目标不能遵从导入,所以必须是完整的合格路径。 > Target does not respect imports, so the fully qualified path has to be used. 如果它是一个类或者抽象类型,访问修正延伸到所有这个类型的字段。同样的,如果它是一个包,访问修正延伸到所有这个包的类型和递归到所有这些类型的字段。 > If it is a class or abstract type, access modification extends to all fields of that type. Likewise, if it is a package, access modification extends to all types of that package and recursively to all fields of these types. ~~~ @:allow(Main) class MyClass { static private var foo: Int; } class Main { static public function main() { MyClass.foo; } } ~~~ 这里 ,MyClass.foo 可以从main方法中访问,因为 MyClass 被使用 @:allow(Main) 注释。这也可以使用 @:allow(Main.main),这两种方法都可以作为字段foo的注释替代对MyClass的注释: > Here, MyClass.foo can be accessed from the main-method because MyClass is annotated with @:allow(Main). This would also work with @:allow(Main.main) and both versions could alternatively be annotated to the field foo instead of the class MyClass: ~~~ class MyClass { @:allow(Main.main) static private var foo: Int; } class Main { static public function main() { MyClass.foo; } } ~~~ 如果一个类型不能被修改为允许这类的访问,访问方法可以禁止访问: > If a type cannot be modified to allow this kind of access, the accessing method may force access: ~~~ class MyClass { static private var foo: Int; } class Main { @:access(MyClass.foo) static public function main() { MyClass.foo; } } ~~~ @:access(MyClass.foo) 注释在main方法中有效的颠覆了foo字段的可见性。 > The @:access(MyClass.foo) annotation effectively subverts the visibility of the foo field within the main-method. **元数据的选择** >[warning] 花絮:元数据的选择 访问控制语言特性使用 Haxe元数据语法,而不是额外的语言特定语法。有如下几个理由: 另外的语法通常使得语言解析更加复杂,也会添加太多关键字。 另外的语法需要另外的学习成本,元数据语法是已知的知识。 元数据语法足够灵活来扩展这个功能。 元数据可以被通过Haxe的宏 访问/生成/修改。 当然,使用元数据语法主要的缺点是,如果你拼错元数据的关键字(例如@:acesss)或者类/包的名将不会得到错误报告。然而,通过这个功能,当你尝试访问一个不被允许的私有字段,你会得到一个错误,毕竟不可能是一个不被察觉的错误。 >[warning] **Trivia**: On the choice of metadata The access control language feature uses the Haxe metadata syntax instead of additional language-specific syntax. There are several reasons for that: > * Additional syntax often adds complexity to the language parsing, and also adds (too) many keywords. > * Additional syntax requires additional learning by the language user,whereas metadata syntax is something that is already known. > * The metadata syntax is flexible enough to allow extension of this feature. > * The metadata can be accessed/generated/modified by Haxe macros. > Of course, the main drawback of using metadata syntax is that you get no error report in case you misspell either the metadata key (@:acesss for instance) or the class/package name. However, with this feature you will get an error when you try to access a private field that you are not allowed to, therefore there is no possibility for silent errors. **Haxe 3.1.0 以后** > **Since Haxe 3.1.0** 如果允许访问一个 接口(第2.3.3节),它延伸到所有实现这个接口的类: > If access is allowed to an interface(2.3.3),it extends to all classes implementing that interface: ~~~ class MyClass { @:allow(I) static private var foo: Int; } interface I { } class Main implements I { static public function main() { MyClass.foo; } } ~~~ 对于访问授权父类,也是同样的,这种情况会延伸到所有的子类: > This is also true for access granted to parent classes, in which case it extends to all child classes. **破坏性功能** >[warning] **花絮**:破坏性功能 子类和实现类的访问扩展只支持Haxe 3.0以后。当编写这本手册时发现这部分的访问控制实现是容易缺失的。 >[warning] **Trivia**: Broken feature Access extension to child classes and implementing classes was supposed to work in Haxe 3.0 and even documented accordingly. While writing this manual it was found that this part of the access control implementation was simply missing.