定义 类字段: 一个类字段是类中一个静态或非静态的变量、属性、或方法。非静态字段被称为成员字段。比如,我们会说 “一个静态方法” 或 “一个成员变量”。 [warning] Definition: Class Field A class field is a variable, property or method of a class which can either be static or nonstatic. Non-static fields are referred to as member fields, so we speak of e.g. a static method or a member variable.
在前面我们已经了解了 Haxe 程序中的类型是如何构成的,在类字段这一章中我们将结束 Haxe 的 “结构” 部分,并引申至其 “行为” 的部分,这是因为类字段即是 表达式 的归宿。
So far we have seen how types and Haxe programs, in general, are structured. This section about class fields concludes the structural part and at the same time bridges to the behavioral part of Haxe. This is because class fields are the place where expressions are at home.
有三种类字段:
There are three kinds of class fields:
- 变量:变量(第4.1节)字段用于保存某种类型的值,可以被读取或者写入。
- 属性:属性(第4.2节)字段对从类之外的访问行为自定了一组访问规则,他们看起来像是一个变量字段。
- 方法:方法(第4.3节)是一个函数,可以被调用以执行一些代码。
- Variable: A variable(4.1) class field holds a value of a certain type,which can be read or written.
- Property: A property (4.2) class field defines a custom access behavior for something that, outside the class, looks like a variable field.
- Method: A method (4.3) is a function which can be called to execute code.
严格说,一个变量可以被认为是一个属性带有某个访问修饰符。事实上,Haxe编译器在它们的类型解析时并不区分变量和属性,但是它们仍然在语法层面上有所区分。
Strictly speaking, a variable could be considered to be a property with certain access modifiers. Indeed,the Haxe Compiler does not distinguish variables and properties during its typing phase, but they remain separated at syntax level.
术语层面上讲,方法是归属于一个类的(静态或非静态)函数。而诸如出现于表达式中一个局部函数(第5.11节)则不被认为是方法。
Regarding terminology,a method is a (static or non-static) function belonging to a class. Other functions, such as a local functions (5.11) in expressions, are not considered methods.