forked from R3D9477/haxe-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hx
84 lines (61 loc) · 2.66 KB
/
Main.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package;
// http://api.haxe.org/Type.html
// http://old.haxe.org/doc/cross/reflect
class A {
public function new () {
trace("Constructor of the class A");
}
public function func () {
trace("Call method 'func' of the class A");
}
public function func1 (a : Int, b : Int) {
trace("Call method 'func1' of the class A with arguments: " + a + " + " + b + " = " + (a + b));
return a + b;
}
}
class Main {
public static function main () {
//----------------------------------------------------------------------
trace("--- Make anonymous structure ---");
var a = { one: 1, two: 2.2, three: "три", four: { one: "number one" } };
trace(a);
//----------------------------------------------------------------------
trace("--- Check availability of the field by name ---");
trace("The object 'a' contains 'one': " + Reflect.hasField(a, "one"));
trace("The object 'a' contains 'five': " + Reflect.hasField(a, "five"));
//----------------------------------------------------------------------
trace("--- Display all fields of object 'a' ---");
for(aField in Reflect.fields(a)) {
trace("a." + aField + " = " + Reflect.field(a, aField));
}
//----------------------------------------------------------------------
trace("--- Copy object 'a' ---");
var b = Reflect.copy(a);
trace(b);
//----------------------------------------------------------------------
trace("--- Display all fields of object 'b' ---");
for(bField in Reflect.fields(b)) {
trace("b." + bField + " = " + Reflect.field(b, bField));
}
//----------------------------------------------------------------------
trace("--- Change object 'a' ---");
a.two = 2.2222; // not affect the copy 'b' of the object 'a'
a.three = "THREE"; // not affect the copy 'b' of the object 'a'
a.four.one = "NUMBER ONE"; // affect the copy 'b' of the object 'a'!!!
//----------------------------------------------------------------------
trace("--- Display all fields of object 'a' after changing ---");
for(aField in Reflect.fields(a)) {
trace("a." + aField + " = " + Reflect.field(a, aField));
}
//----------------------------------------------------------------------
trace("--- Display all fields of object 'b' after changing object 'a' ---");
for(bField in Reflect.fields(b)) {
trace("b." + bField + " = " + Reflect.field(b, bField));
}
//----------------------------------------------------------------------
trace("--- Call method of object by name ---");
var c = new A();
Reflect.callMethod(c, Reflect.field(c, "func"), []);
trace("Result of method 'c.func1': " + Reflect.callMethod(c, Reflect.field(c, "func1"), [1, 2]));
}
}