Microsoft Dynamics AX 2009 : Classes and Interfaces

You define types and their structure in the AOT, not in the X++ language. Other programming languages that support type declarations do so within code, but Dynamics AX supports an object layering feature that accepts X++ source code customizations to type declaration parts that comprise variable declarations and method declarations. Each part of a type declaration is managed as a separate compilation unit, and model data is used to manage, persist, and reconstitute dynamic types whose parts can comprise compilation units from many object layers.

You use X++ to define logic, including method profiles (return value, method name, and parameter type and name). The X++ editor allows you to add new methods to the AOT, so you can continue to use the X++ editor while constructing types.

You use X++ class declarations to declare protected instance variable fields that are members of application logic and framework reference types. You can’t declare private or public variable fields. You can declare classes abstract if they are incomplete type specifications that can’t be instantiated. You can also declare them final if they are complete specifications that can’t be further specialized. The following code provides an example of an abstract class declaration header.

abstract class MyClass
{
}


You can also structure classes into single-inheritance generalization or specialization hierarchies in which derived classes inherit and override members of base classes. The following code shows an example of a derived class declaration header that specifies that MyDerivedClass extends the abstract base class MyClass. It also specifies that MyDerivedClass is final and can’t be further specialized by another class. Because X++ doesn’t support multiple inheritance, derived classes can extend only one base class.

final class MyDerivedClass extends MyClass
{
}


X++ also supports interface type specifications that specify method signatures but don’t define their implementation. Classes can implement more than one interface, but the class and its derived classes should together provide definitions for the methods declared in all the interfaces. If it fails to provide the method definitions, the class itself is marked as abstract. The following code provides an example of an interface declaration header and a class declaration header that implements the interface.

interface MyInterface
{
    void myMethod();
}
class MyClass implements MyInterface
{
    void myMethod()
    {
    }
}


Fields

A field is a class member that represents a variable and its type. Fields are declared in class declaration headers; each class and interface has a definition part with the name classDecla-ration in the AOT. Fields are accessible only to code statements that are part of the class declaration or derived class declarations. Assignment statements are not allowed in class declaration headers. The following example demonstrates how variables are initialized with assignment statements in a new method.

class MyClass
{
    str s;
    int i;
    MyClass1 myClass1;

    public void new()
    {
        i = 0;
        myClass1 = new MyClass1();
    }
}


Methods

A method on a class is a member that uses statements to define the behavior of an object. An interface method is a member that declares an expected behavior of an object. The following code provides an example of a method declaration on an interface and an implementation of the method on a class that implements the interface.

interface MyInterface
{
    public str myMethod()
    {
    }
}
class myClass implements MyInterface
{
    public str myMethod();
    {
        return "Hello World";
     }
}


Methods are defined with public, private, or protected access modifiers. Methods are publicly accessible by default. Additional method modifiers supported by X++ are provided in Table 1.

Table 1. Method Modifiers Supported by X++
Modifier Description
static Static methods are accessed via class declarations. Fields can’t be accessed from within a static method.
final Final methods can’t be overridden by methods with the same name in derived classes.
abstract Abstract methods have no implementation. Derived classes must provide definitions for abstract methods.
server Server methods can execute only on an Application Object Server. The server modifier is allowed only on static methods.
client Client methods can execute only on a MorphX client. The client modifiers are allowed only on static methods.
display Display methods are invoked each time a form or report is redrawn. The display modifier is allowed only on table, form, form data, report, and report design methods.
edit The edit method is invoked each time a form is redrawn or a user provides input through a form control. The edit modifier is allowed only on table, form, and form data source methods.

Method parameters can have default values that are used when parameters are omitted from method invocations. The following code sample prints “Hello World” when myMethod is invoked with no parameters.

public void myMethod(str s = "Hello World")
{
    print s;
    pause;
}

public static void main(Args _args);
{
    myMethod();
}


A constructor is a special instance method that is invoked to initialize an object when the new operator is executed by the Dynamics AX runtime. You can’t call constructors directly from X++ code. The following sample provides an example of a class declaration header and an instance constructor method that takes one parameter as an argument.

class myClass
{
    int i;

    public void new(int _i)
    {
        i = _i;
    }
}