class
- Last UpdatedApr 24, 2024
- 2 minute read
The class keyword is used to declare a class.
-
Classes implicitly have public access and class members are public by default.
-
If a class is declared outside a namespace, it is automatically assigned to the global namespace.
-
Nested classes are not supported.
Code example
The following example shows how to declare class variables, constructors and methods.
namespace Test
{
class TestClass
{
public int val_1; //an int instance variable
public static string val_2; //a string class variable
private double val_3;
//this is the constructor
TestClass()
{
int val_4 = 0; // an int local variable
val_1 = 2;
val_2 = "hello";
val_3 = 1;
}
public static void testInstance()
{
// can be accessed with Test::TestClass.testInstance()
}
private void testInstance2()
{
// can be accessed only within the body of the class
}
}
}
Guidelines
-
Class variables do not support the assignment operation at declaration time and are initialized to their default value, based on their type.
-
The constructor is a method whose name is the same as the class and without a return type. Classes do not support multiple constructors or constructor with an input parameters list.
-
Whenever an instance of a class is created using the ScriptObject node, its constructor is called. If constructor method is not declared, a base constructor is called and class variables are initialized to their default value based on their type.
-
Destructors (or finalizers) do not exist.
-
Methods are code blocks that contain a series of imperative statements to be executed by calling the method itself. Method signature is composed of the access level, such as public or private (see Access modifiers), the static keyword (optional), the return value type, the name of the method and a list of parameters, enclosed in parentheses and comma separated. Empty parentheses indicate that the method requires no parameters.
-
Method overloading is not currently supported, that is, each method must have a unique name within a defined class.
-
For each defined class in an included XRS script, the Parser creates a static ScriptClass node that represents all its static members (if any).