Learning Haxe: Part 3: Objects

Learning Haxe

Haxe is a strongly-typed multi-paradigm programming language that also supports cross-platform compiling into many different source and byte-code languages such as ActionScript 3, JavaScript, Java, C++, C#, PHP, Python, Lua, and Node.js.


Objects

Haxe is not purely an object-oriented programming language. However, it easily supports the object-oriented programming paradigm through its class and instance functionality.

Classes

Haxe works through classes, object blueprints for functionality.

The entry of a project is through its Main class. This may not always literally be named “Main,” but is where code execution should start.

Note: When running Haxe from the command-line, the option “-main” marks which class should be this starting point.

Field Types

Classes have three different types of fields: Variables, Properties, or Methods.

Variables

Object can hold variables. By default, these are private to the object and can be referenced through the this keyword.

Properties

A property is variable that is public. It is marked as such within the object with the keyword public. It can be accessed outside of the object through dot-notation.

Methods

In Haxe, a method is a function that can be accessed outside of an object. Much like variables in this way, functions must use the keyword public to signal that it can be used outside of the object.

Class Instances

Creating a new copy of an object is called a class instance in Haxe. This is done through the function new().

In programming terminology, the new() function is a constructor. This “constructs” a new object.

In Haxe, a new copy of an object is an instance of that class. When the public keyword is used with a new() function (a method), it allows for creating a new object through sending data into the object as arguments to the function.

Public and Private

As noted with the field types variables and methods, they are private by default. A variable or function is not available outside the object unless the specific keyword public is used.

To be used outside the object, a function (method) must be public. This is the same with a variable (property) that is labeled in the same way.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.