Skip to content

Quick start

Richard Baltrusch edited this page Mar 9, 2021 · 4 revisions

Quick start

  1. Class definition
  2. Instance attributes
  3. Instantiating objects
  4. Calling methods
  5. Deleting objects
  6. Accessing attributes
  7. More information

Class definition

The shortest barebones class definition in objectbatch contains a class header and a class constructor, as follows:

call class %*
%class%

:public-construct --> 1:objectname
    call super %*
exit /b

Instance attributes

We may declare one or more instance attributes in the constructor method (public-construct) using the following syntax:

:public-construct --> 1:objectname
    call super %*
    set %self%.myAttr1=something
    set %self%.myAttr2=somethingelse
exit /b

Note that we can only declare instance attributes in the constructor after the superclass call, as this will instantiate our object by inheriting from the base object.

Instantiating objects

To instantiate a new object myobj of type MyClass, we may use the following syntax:

call new myClass myobj construct

The example above constructs a new object myobj of type MyClass and makes sure it is correctly instantiated using the new keyword.

Calling methods

To call a public method of a given class MyClass, for example the constructor method construct, for a given object obj we may use the following syntax:

call MyClass obj construct

If an object is already instantiated, we may use the # syntax below:

call # obj construct

The example above is a shorthand notation that exploits the __class__ attribute of any object to call the correct class method.

Passing input arguments to methods

To pass input arguments to public methods from outside the class, we may simply use the normal method call syntax and add any extra arguments, as follows:

call Class object method arg1 arg2 ...

Deleting objects

To remove a given object obj from the batch environment, we may use the following syntax:

call clear obj

Accessing attributes

Writing attributes

To write a value to an object attribute myAttr for object obj, we may use the following syntax:

set %obj%.myAttr=2

Reading attributes

To read the value of an object attribute myAttr for object obj, we may use the following syntax (using delayed expansion):

echo !%obj%.myAttr!

To avoid using delayed expansion, use getattr.

More information

More information can be found in the home page, the quick start page and the examples.