ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
我们已经在前面章节的一些代码示例中看过变量字段了。变量字段保存值,是它们和大多数(非所有)属性共有的特点: > We have already seen variable fields in several code examples of previous sections. Variable fields hold values, a characteristic which they share with most (but not all) properties: ~~~ class Main { static var member:String = "bar"; public static function main() { trace(member); member = "foo"; trace(member); } } ~~~ 从这里我们可以了解到关于变量的几点: > We can learn from this that a variable 1. 有一个名称(member) 2. 有一个类型(String) 3. 可能有一个常量值的初始化(“bar”) 4. 可能有访问修饰符(第4.4节)(static) > 1. has a name (here: member), > 2. has a type (here: String), > 3. may have a constant initialization (here: "bar") and > 4. may have access modifiers (4.4) (here: static) 例子首先输出member的初始值,然后在输出新的值之前设置它为 foo 。访问修饰符的影响是在所有三种类之间共享,将在单独的章节讲解。 > The example first prints the initialization value of member,then sets it to "foo" before printing its new value. The effect of access modifiers is shared by all three class field kinds and explained in a separate section. 可能注意到,显式的类型并不是必须的,如果有一个初始化的值。这个情况下编译器会推断它为恰当的类型。 > It should be noted that the explicit type is not required if there is an initialization value. The compiler will infer (3.6) it in this case. ![](https://box.kancloud.cn/2016-07-30_579cc7f20c459.png)