企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
变量保存数据,方法被定义程序行为通过托管表达式(第5章)。我们已经在每个代码例子中看过方法字段,前面的 HelloWorld示例就包含一个 main 方法: > While variables (4.1) hold data, methods are defining behavior of a program by hosting expressions (5). We have seen method fields in every code example of this document with even the initial Hello World (1.3) example containing a main method: ~~~ class Main { static public function main():Void { trace("Hello World"); } } ~~~ 方法通过 function 关键字识别。我们还可以了解到,它们: > Methods are identified by the function keyword. We can also learn that they 1. 有一个名字(这里是main) 2. 有一个参数列表(这里为 empty()) 3. 有一个返回类型(这里是 Void) 4. 可能有访问修饰符(第4.4节)(这里是 static 和public) 5. 可能有一个表达式(这里是 {trace("Hello World");}) > 1. have a name (here: main), > 2. have an argument list (here: empty ()), > 3. have a return type (here: Void), > 4. may have access modifiers (4.4) (here: static and public) and > 5. may have an expression (here: {trace("Hello World");}). 还可以看下面的例子,了解更多参数和返回类型的知识: > We can also look at the next example to learn more about arguments and return types: ~~~ class Main { static public function main() { myFunc("foo", 1); } static function myFunc(f:String, i) { return true; } } ~~~ 参数通过字段名后一个开口的 ( 括号开始,一个 逗号 , 作为参数列表中每个参数的分隔符号,然后跟一个闭口的 ) 括号。参数规范的附加信息在 函数类型(第2.6节)中描述。 > Arguments are given by an opening parenthesis ( after the field name, a comma , separated list of argument specifications and a closing parenthesis ). Additional information on the argument specification is described in Function Type (Section 2.6). 例子展示了类型推断如何被使用到两个参数和返回类型上。方法 myFunc 有两个参数,但是第一个被显式赋予类型,f,为String类型。第二个参数 i ,没有类型示意,留给编译器从它的调用中推断它的类型。此外,返回类型通过return ture 表达式来推断为 Bool 。 > The example demonstrates how type inference(3.6) can be used for both argument and return types. The method myFunc has two arguments but only explicitly gives the type of the first one, f, as String. The second one, i, is not type-hinted and it is left to the compiler to infer its type from calls made to it. Likewise,the return type of the method is inferred from the return true expression as Bool.