2005/5/10

     
 

CfgScript Class Method

artefaktur

| ClassDeclaration | Class Methods | Class Members | Calling Interface |

CfgScript provides static and nonstatic methods, Constructors and operator overloading.



Content of this chapter:

   ClassMethod
     Constructor
     Destructor/finalize
     Method
     ParameterDecl
     OperatorMethod
   Free Functions
   Samples



 ClassMethod

ClassMethod
:  Constructor
|  Destructor
|  Method
|  OperatorMethod
;

 Constructor

Constructor
: Identifier '('  ParameterDecl ')  BlockStatement
;
A constructor method has the same name as the Class.
It can be used to intialize a new object.

class MyClass
extends MyBaseClass
{
  int ivar;
  MyClass(int intitval) 
  { 
    super("initValForMyBase"); // call MyBaseClass constructor
    ivar = intitval;
  }
  
}

Base class initialization must be the first statement in the MethodBody of the constructor.


 Destructor/finalize

An destructor is implemented as a virtual method named finalize.
This finalize() method will be called once before the object will be destroyed.
Equally to Java you have to call the super.finalize().

class AClass {
  AClass() {}
  void finalize()
  {
    out.println("AClass.finalize()");
  }
}

class BClass
extends AClass
{
  BClass() { super(); }
  void finalize()
  {
    out.println("BClass.finalize()");
    super.finalize();
  }
}
AClass cls = new BClass();
cls = Nil;
The output will be:
BClass.finalize()
AClass.finalize()

 Method

Method
: (MethodAttribute)* TypeName Identifier '(' ParameterDecl ')  BlockStatement
;

ParameterDecl
: 'public'
| 'protected'
| 'private'
| 'static'
;
Static methods can be called without an object instance.

public, protected, private are currently not supported.

Please refer also to  Calling Interface.

 ParameterDecl

ParameterDecl
: [ NormalParameter ( ',' NormalParameter )* ( ', ' InitializedParameter )*
;

NormalParameter:
: ParameterAttribute*  TypeName Identifier
;

InitializedParameter:
: TypeName Identifier '=' Expression
;

ParameterAttribute
: 'in'      // parameter is a in parameter. 
            // A parameter without Attribute is also an in parameter
| 'out'     // out parameter (value can changed by callee)
| 'inout'   // combination of in and out
| 'byref'   // only used in rdmi: Parameter should be tranfered by reference
| 'byval'   // only used by rdmi: Parameter should be tranfered by value
;

Please refer also to  Calling Interface.

 OperatorMethod

OperatorMethod
: (MethodAttribute)* TypeName 'operator' Operator '(' ParameterDecl ')  BlockStatement
;
Sample:

class Average
extends acdk.lang.Object
{
  int count;
  double average;
  
  // a + operator should return this to be chainable
  // see below where this operator will be used
  Average operator+(int v) // defines the + operator
  {
    average = (average * count + v) / (count + 1);
    count = count + 1;
    return this; 
  }
  Average(int v) 
  { 
    count = 1;
    average = v;
  }
  int getAverage() { return average; }
  int getCount()  return ivar; 
}

Average av = new Average(2);
av = av + 1 + 6; // calls Average.operator+(1).operator+(6)
out.println("Average of 2, 1 and 6 is " + av.getAverage());



Please refer also to  Calling Interface.

 Free Functions

Functions, which are not elements of a class, are not supported by CfgScript.

But you can receive a quite similar result using the  using statement.


// some global functions
using acdk.cfgscript.ScriptGlobals;

// byRef is a static function of ScriptGlobals
MyClass.staticFunction(byRef(42));



 Samples