声明一个变量。
// Syntax for declaring a variable of global scope or function scope. var name1 [: type1] [= value1] [, ... [, nameN [: typeN] [= valueN] ]] // Syntax for declaring a variable field within a class. [attributes] [modifiers] var name1 [: type1] [= value1] [, ... [, nameN [: typeN] [= valueN].]]
- attributes
可选项。控制字段的可见性和行为的属性。
- modifiers
可选项。控制字段的可见性和行为的修饰符。
- name1, ..., nameN
必选。被声明的变量的名字。
- type1, ..., typeN
可选项。所声明的变量的类型。
- value1, ..., valueN
可选项。赋给变量的初始值。
使用 var 语句来声明变量。变量可以绑定到特定的数据类型来确保类型安全。当声明变量时,可以给这些变量赋值,并且这些值可以在以后从脚本中更改。未经显式初始化的变量被赋予默认值 undefined(必要时强迫转换为此变量的类型)。
类中的变量字段类似于全局或函数变量,不同的只是它的作用范围是该类,而且它可以用多个修饰符来指定其可见性和用法。
示例下面的示例阐释了 var 语句的某些用法。
class Simple { // A field declaration of the private Object myField. private var myField : Object; // Define sharedField to be a static, public field. // Only one copy exists, and is shared by all instances of the class. static public var sharedField : int = 42; } var index; var name : String = "Thomas Jefferson"; var answer : int = 42, counter, numpages = 10; var simpleInst : Simple = new Simple;要求请参见
参考
const 语句function 语句
new 运算符
概念
变量和常数的范围类型批注