FANDOM


You can create a class definition using the following syntax:

public class ClassName
{
    declarations;
}

Classes must be public. That is, they can be used by all bots in a mission.

Class members can be public or private. A private member can't be accessed by anything other than the object itself, while a public member can be accessed from outside the object. Members are public by default.

Class members can be fields or functions (also called methods). For example, the following class contains 4 fields (a, b, x and s) and one method (MyFunction).

public class MyClass
{
    int a, b;
    float x = 3.33;
    string s = "hello";
    float MyFunction( float value )
    {
        return (value*x)-1;
    }
}

As shown in this example, the class members can be initialized (x=3.33). You can also define a constructor, which is a special method having the same name as the class name. This method will be called automatically at creation time of a class instance. You can also declare more than one method with the same name but different parameters.

   public class MyClass
   {
    int a, b;
    void MyClass( )
    {
    a = 2; b = 3;
    }
    void MyClass( int a, int b )
    {
    this.a = a; this.b = b;
    }
   }


In this example two constructors are declared for MyClass, one without parameters and the other one with two parameters. As the names of the parameters of the second constructor are the same as the names of the two members a et b we must use the this.a and this.b to avoid confusion with the parameters. Another more simpler solution would be to give different names to the parameters.


   void Test( )
   {
    MyClass item1(); // constr. w/o parameters
    MyClass item2(4, 5); // constr. with 2 parameters
    MyClass item3; // no constructor called,
    // therefore item3 == null
   }


You can also define a destructor. This must be a void fonction without parameters that has the same name as the class name but prefixed by the ~ character. The destructor is called automatically as soon as the class instance is no more referenced by anyone.


   public class MyClass
   {
    static private int counter = 0; // instance counter
    void MyClass( )
    {
    counter ++; // one instance more
    }
    void ~MyClass( )
    {
    counter --; // one instance less
    }
   }
   void Test()
   {
    MyClass item1( ); // counter = 1
    MyClass item2( ); // counter = 2
    item1 = null; // counter = 1
   } // counter = 0


If you pass a class instance as parameter to a function, the function only receives a pointer to the instance. That means if you modify the instance in the function, the instance that has been passed to the function will be actuallay modified.

See alsoEdit

public, private, static, synchro, new, pointer, this, programming, type and category.

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.