Objects and Classes in D365
1. Objects
• Use the classes within Microsoft Dynamics 365 X++ development.
• Control access to methods using Access Control Method Modifiers.
• Extend a class using the concept of inheritance.
• Describe the differences between an object and a class.
• Initialize variables in the appropriate place according to scoping rules.
• Call methods within the same class.
• Use the different method types available.
• Describe the similarities and differences between tables and classes.
• Use the eventing publisher and subscriber model when modifying code in the application.
2. Classes
A class is constructed of members, which can be variables or methods.
• Variables are used to store the data for the class. They are specific to an object; every object instantiated from the class declaration has its copy of the variable. Such variables are known as instance variables.
• Methods are the functions that operate on the data and define the object's behavior. They are often declared to operate on the instance variables of the class and are known as instance methods.
A class is not an object. A class is an outline that defines how an object behaves when the object is created from the class. Obtain concrete objects by instantiating a previously defined class. Many objects can be instantiated from one class definition.
A class contains the properties, methods, and variables needed by the object. Create a class by right-clicking the Classes node of the AOT and selecting New Class, or by using the Class Wizard at Tools> Development Tools> Wizards. A class always contains a class declaration node and two methods:
• Class Declaration: Specifies the name of the class and necessary variables. It can also specify inheritance. NOTE: class declaration is not a method. It is a class-level scope that is used to define variables that are global to all non-static methods within the class. This means that you can use these variables in any method in the class, and the variable value will be the same in each method.
• New(): This method instantiates a new object. You can also assign values to object variables using this method.
• Finalize(): This method terminates the object. It is never used within the application and exists only for convention. These methods enable object instantiation to occur. An infinite number of methods can be added to a class.
2.1 Method and access control
Methods in a class are always available to other methods in the class. However, they should not always be available to methods in other classes. To control access to a method, a methods modifier is placed in the method definition.
Modifiers
There are three modifiers available.
• Public allows the method to be called from any code in the application. • Protected allows the method to be called only by methods in the same class or subclasses of the class in which the method is defined.
• Private allows the method to be called only by methods in the same class in which the method is defined.
When a new method is created, the default modifier of the method is Private.
2.2 Inheritance
Inheritance is a concept where one class can inherit all the methods and variables from another class. A child class inherits the methods of the parent class. Additional methods can be applied to the child class, and inherited methods can be overridden. This means the child class is not always identical to the parent class.
The advantage of inheritance in object-oriented programming is that code can be written one time and be reused many times.
NOTE: In X++ one class can extend another class, and one extended data type can extend another.
Extend a Class To have a class extend another class, modify the class declaration code of the child class by adding extends and then the parent class, shown as follows:
Objects created from the Child
• Have at least the same methods and variables as the Parent.
• Can have methods and variables that do not exist in the Parent.
• Can have methods from the Parent that are overridden or altered, in the Child.
For example, if the Parent contains four methods:
Method1, Method2, Method3, Method4
The Child can have two of those methods overridden, plus an additional method.
Method1, Method3, Method5
If the code refers to one of the methods in the Child that has not been overridden, the system will go to the Parent to retrieve the method. If that method then calls a method that is in the Child, it will revert to the Child.
3. Objects
Objects are created at run-time using the classes defined in the AOT. To create an object from a class, the class has to be instantiated.
3.1 Instantiating a class
Object instance methods can only be used when an object is instantiated from a specific class. To instantiate a class, it isto create a new instance of it. You can have multiple instances of a class, meaning, you can have the same code running multiple times. Think of how in Microsoft Word, you can have two documents open at the same time. They are both running the same program, but are two separate instances of that program. When you create a new object, call the new() method to execute the code when the object is instantiated.
Once the class is instantiated, to execute a specific method from your code, use the reference variable followed by a "dot" operator and then the method name.
Comments
Post a Comment