HaxeDoc2

(やまだぃちぅ) #1

  • Enclosed in curly braces{}are the class fields,

  • Which consist of twovariablefieldsxandyof typeInt,

  • followed by a specialfunctionfield namednew, which is theconstructorof the class,

  • as well as a normal functiontoString.


There is a special type in Haxe which is compatible with all classes:

Type:Class<T>
This type is compatible with all class types which means that all classes (not their instances)
can be assigned to it. At compile-time,Class<T>is the common base type of all class types.
However, this relation is not reflected in generated code.
This type is useful when an API requires a value to beaclass, but not a specific one. This
applies to several methods of the Haxe reflection API (10.6).

2.3.1 Class constructor


Instances of classes are created by calling the class constructor - a process commonly referred to
asinstantiation. Another name for class instances isobject. Nevertheless, we prefer the term class
instance to emphasize the analogy between classes/class instances and enums/enum instances
(2.4).

1 var p = new Point(-1, 65);


This will yield an instance of classPoint, which is assigned to a variable namedp. The construc-
tor ofPointreceives the two arguments-1and 65 and assigns them to the instance variablesx
andyrespectively (compare its definition inClass Instance(Section 2.3)). We will revisit the ex-
act meaning of thenewexpression later in the section5.12. For now, we just think of it as calling
the class constructor and returning the appropriate object.

2.3.2 Inheritance......................................


Classes may inherit from other classes, which in Haxe is denoted by theextendskeyword:

1 class Point3 extends Point {
2 var z : Int;
3 public function new(x,y,z){
4 super(x,y);
5 this.z = z;
6 }
7 }


This relation is often described as ”is-a”: Any instance of classPoint3is also an instance of
Point. Pointis then known as theparent classofPoint3, which is achild classofPoint.A
class may have many child classes, but only one parent class. The term “a parent class of class
X” usually refers to its direct parent class, the parent class of its parent class and so on.
The code above is very similar to the originalPointclass, with two new constructs being
shown:


  • extends Pointdenotes that this class inherits from classPoint

  • super(x, y)is the call to the constructor of the parent class, in this casePoint.new

Free download pdf