73 lines
3.1 KiB
Plaintext
73 lines
3.1 KiB
Plaintext
\b;Instruction \c;class\n;
|
|
This allows you to declare a class definition using following syntax:
|
|
\c;
|
|
\s;public class ClassName
|
|
\s;{
|
|
\s; declarations;
|
|
\s;}
|
|
\n;
|
|
Classes can only be \l;public\u cbot\public;, that is they can be used from all bots in a mission. Class members are also public, that is they are accessible from outside the class. Class members can be fields or functions (also called methods), for example the followin class \c;MyClass\n; contains 4 fields (a, b, x and s) and one method (MyFunction).
|
|
\c;
|
|
\s;public class MyClass
|
|
\s;{
|
|
\s; int a, b;
|
|
\s; float x = 3.33;
|
|
\s; string s = "hello";
|
|
\s; float MyFunction( float value )
|
|
\s; {
|
|
\s; return (value*x)-1;
|
|
\s; }
|
|
\s;}
|
|
\n;
|
|
As shown in this exemple the class members can be initialized (\c;x=3.33\n;). 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.
|
|
\c;
|
|
\s;public class MyClass
|
|
\s;{
|
|
\s; int a, b;
|
|
\s; void MyClass( )
|
|
\s; {
|
|
\s; a = 2; b = 3;
|
|
\s; }
|
|
\s; void MyClass( int a, int b )
|
|
\s; {
|
|
\s; this.a = a; this.b = b;
|
|
\s; }
|
|
\s;}
|
|
\n;
|
|
In this example two constructors are declared for \c;MyClass\n;, 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 \c;a\n; et \c;b\n; we must use the \c;\l;this\u cbot\this;.a\n; and \c;\l;this\u cbot\this;.b\n; to avoid confusion with the parameters. Another more simpler solution would be to give different names to the parameters.
|
|
\c;
|
|
\s;void Test( )
|
|
\s;{
|
|
\s; MyClass item1(); // constr. w/o parameters
|
|
\s; MyClass item2(4, 5); // constr. with 2 parameters
|
|
\s; MyClass item3; // no constructor called,
|
|
\s; // therefore item3 == null
|
|
\s;}
|
|
\n;
|
|
You can also define a destructor. This must be a \c;void\n; 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.
|
|
\c;
|
|
\s;public class MyClass
|
|
\s;{
|
|
\s; static private int counter = 0; // instance counter
|
|
\s; void MyClass( )
|
|
\s; {
|
|
\s; counter ++; // one instance more
|
|
\s; }
|
|
\s; void ~MyClass( )
|
|
\s; {
|
|
\s; counter --; // one instance less
|
|
\s; }
|
|
\s;}
|
|
\s;void Test()
|
|
\s;{
|
|
\s; MyClass item1( ); // counter = 1
|
|
\s; MyClass item2( ); // counter = 2
|
|
\s; item1 = null; // counter = 1
|
|
\s;} // counter = 0
|
|
\n;
|
|
If you pass a class instance as parameter to a \l;function\u cbot\function;, the function only receives a \l;reference\u cbot\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.
|
|
|
|
\t;See also
|
|
\c;\l;public\u cbot\public;\n;, \c;\l;private\u cbot\private;\n;, \c;\l;static\u cbot\static;\n;, \c;\l;synchronized\u cbot\synchro;\n;, \c;\l;new\u cbot\new;\n;, \c;\l;reference\u cbot\pointer;\n;, \c;\l;this\u cbot\this;\n;
|
|
\l;Programming\u cbot;, \l;types\u cbot\type; and \l;categories\u cbot\category;.
|