2005/5/10

     
 

CfgScript Class Declaration

artefaktur

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

CfgScript is a object oriented language based on the class model of Java/C#.



Content of this chapter:

   ClassDeclaration
   SuperDecl
   ClassBody
   Interface
   Reflection



 ClassDeclaration

ClassDeclaration
: ( 'class' | 'interface' )  ComponentTypeName
    SuperDecl
  '{'  ClassBody '}'
;

 SuperDecl

SuperDecl
: [ 'extends'  ComponentTypeName ]
  [ 'implements'  ComponentTypeName ( ','  ComponentTypeName)* ]
;


// class is in namespace/unit/module mymodule
class mymodule.MyClass
extends acdk.lang.Object // is derived from acdk.lang.Object
implements acdk.lang.Comparable, acdk.io.Serializable // and implements 2 interfaces
{
}

See also:  Constructor.

 ClassBody

ClassBody
: (  ClassMember |  ClassMethod )*
;

 Interface

It is possible to declare an interface in CfgScript:

interface mymodule.MyInterface
{
  MyInterface() {}
  void doIt();
  // alternative:
  void doItAlso() = 0;;
}

class mymodule.MyClass
extends acdk.lang.Object
implements mymodule.MyInterface
{
  MyClass() {}
  void doIt()
  {
  }
  void doItAlso()
  {
  }
}
mymodule.MyInterface mint = new mymodule.MyClass();
mint.doIt();

Different to C++/Java/C# CfgScript doesn't ensure at compile time, that all interface methods has also an implementation in the created object class.
If you try to call an interface method, which doesn't have an implementation an exception will be thrown.

 Reflection

Each CfgScript class is also a ACDK class. So meta features, like reflection can be used on CfgScript classes.

Sample:

class AClass 
extends acdk.lang.Object
{
  static String svar;
  int ivar;
  AClass(int i = 3) { ivar = i; }
  String foo() { return svar; }
  int bar() { return ivar; }
}

Class cls = Class.forName("AClass");
out.println("Class name for class is: " + cls.toString());

out.println("Class has following Contructors:");
foreach(Constructor con in cls.getDeclaredConstructors())
{
  out.println(con.toString());  
}

out.println("Class has following Members:");
foreach(Field field in cls.getDeclaredFields())
{
  out.println(field.toString());  
}

out.println("Class has following Methods:");
foreach(Method method in cls.getDeclaredMethods())
{
  out.println(method.toString());  
}