ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
属性的出现有对类型系统的一些影响。最重要的是,有必要理解属性是一个编译时功能,因此需要类型是已知的。如果我们要分配一个类的属性为Dynamic,字段访问不会遵守存取器方法。同样,访问限制不再应用,所有的访问是几乎公开的。 > The presence of properties has several consequences on the type system. Most importantly, it is necessary to understand that properties are a compile-time feature and thus require the types to be known. If we were to assign a class with properties to Dynamic, field access would not respect accessor methods. Likewise,access restrictions no longer apply and all access is virtually public. 当使用get 或者set 访问标识符,编译器确保getter 和setter实际上存在。下面的问题不会编译: > Whenusing get or set accessidentifier,the compiler ensures that the getter and setter actually exists. The following problem does not compile: ~~~ class Main { // Method get_x required by property x is missing public var x(get, null):Int; static public function main() {} } ~~~ 方法 get_x 丢失了,但是它不必被定义到属性自己的类定义中,只要一个父类中定义了它: > The method get_x is missing, but it need not be declared on the class defining the property itself as long as a parent class defines it: ~~~ class Base { public function get_x() return 1; } class Main extends Base { // ok, get_x is declared by parent class public var x(get, null):Int; static public function main() {} } ~~~ dynamic 访问修饰符正常的工作就像get 或者 set,但是不会检查存在与否。 > The dynamic access modifier works exactly like get or set, but does not check for the existence