HaxeDoc2

(やまだぃちぅ) #1
3.have a return type (here:Void),

4.may have access modifiers (4.4) (here:staticandpublic) and

5.may have an expression (here:{trace(trace("Hello World");)}).

We can also look at the next example to learn more about arguments and return types:
1 class Main {
2 static public function main() {
3 myFunc("foo", 1);
4 }
5
6 static function myFunc(f:String, i){
7 return true;
8 }
9 }
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 argu-
ment specification is described inFunction Type(Section 2.6).
The example demonstrates how type inference (3.6) can be used for both argument and return
types. The methodmyFunchas two arguments but only explicitly gives the type of the first one,
f, asString. 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 thereturn true
expression asBool.

4.3.1 Overriding Methods


Overriding fields is instrumental for creating class hierarchies. Many design patterns utilize it,
but here we will explore only the basic functionality. In order to use overrides in a class, it is
required that this class has a parent class (2.3.2). Let us consider the following example:
1 class Base {
2 public function new() { }
3 public function myMethod() {
4 return "Base";
5 }
6 }
7
8 class Child extends Base {
9 public override function myMethod() {
10 return "Child";
11 }
12 }
13
14 class Main {
15 static public function main() {
16 var child:Base = new Child();
17 trace(child.myMethod()); // Child
18 }
19 }


The important components here are
Free download pdf