Generic Classes

1 General Description

There are essentially two cases of generic class parameterization in Modelica: class parameters (formal parameters to the class) can either be instance parameters, i.e. have instances as values, or be type parameters, i.e. have types as values.

Formal class parameters are replaceable variable or type declarations within the class (usually) marked with the prefix
replaceable. The special case of replaceable local functions is roughly equivalent to virtual methods in some object-oriented programming languages.
Actual arguments to classes are modifiers, which when containing whole variable declarations or types are preceded by the prefix

2 Class Parameters being Component Declarations

2.1 GreenClass and YellowClass

2.2 The Class C

Here we handle the case when class parameters are component being instances or objects. The class C has three class parameters, marked by the keyword replaceable.

Now a class C2 is defined by providing two declarations of obj1 and obj2 as actual arguments to class C, being red and green respectively, instead of the defaults green and yellow. The keyword redeclare must precede an actual argument to a class formal parameter in order to avoid accidentally changing the type of an object through a standard modifier. The type of a class component cannot be changed if it is not declared as replaceable and a redeclaration is provided.

The definition of C2 is equivalent to C2b.

3 Class Parameters being Types

3.1 BlueClass

3.2 The Class C

A class parameter can also be a type, for example in the class C below it is easy to change the color of all objects of the type ColoredClass.

In class
C2 the type parameter of ColoredClass are redeclared as BlueClass.

The definition of
C2 is equivalent to C2b.