HaxeDoc2

(やまだぃちぅ) #1

1 class MyClass {
2 static public function available() {
3 unavailable();
4 }
5 static private function unavailable() { }
6 }
7
8 class Main {
9 static public function main() {
10 MyClass.available();
11 // Cannot access private fieldunavailable
12 MyClass.unavailable();
13 }
14 }


Access to fieldavailableof classMyClassis allowed from withinMainbecause it is de-
noted as beingpublic. However, while access to fieldunavailableis allowed from within
classMyClass, it is not allowed from within classMainbecause it isprivate(explicitly, al-
though this identifier is redundant here).
The example demonstrates visibility throughstaticfields, but the rules for member fields are
equivalent. The following example demonstrates visibility behavior for when inheritance (2.3.2)
is involved.
1 class Base {
2 public function new() { }
3 private function baseField() { }
4 }
5
6 class Child1 extends Base {
7 private function child1Field() { }
8 }
9
10 class Child2 extends Base {
11 public function child2Field() {
12 var child1 = new Child1();
13 child1.baseField();
14 // Cannot access private fieldchild1Field
15 child1.child1Field();
16 }
17 }
18
19 class Main {
20 static public function main() { }
21 }


We can see that access tochild1.baseField()is allowed from withinChild2even though
child1is of a different type,Child1. This is because the field is defined on their common ances-
tor classBase, contrary to fieldchild1Fieldwhich can not be accessed from withinChild2.
Omitting the visibility modifier usually defaults the visibility toprivate, but there are ex-
ceptions where it becomespublicinstead:

1.If the class is declared asextern.

2.If the field id declared on an interface (2.3.3).
Free download pdf