Class

Class[class, super] := {members}
creates a new class with super as parent to inherit from. A missing super in an undefined class is filled with the value of $DefaultClass , usually set to Object.
Class[class]
returns the user input that defined class.
  • Each member of a class may be of the form field = value, field := value, method[parameters] = definition, or method[parameters] := definition. The left hand sides may contain functions used for pattern matching, like Condition. class[parameters] = definition and class[parameters] := definition define a constructor.
  • object.member and object.member[parameters] access a member in the context of object. This.member references a member of the object, while Super.member uses a member above in the hierarchy.
  • Fields can be assigned with object.member = value or object.member := value. SetAttributes[class.member, attributes] allow access, binding and sealing modifications of a member after having defined a class, while SetAttributes[Class[class], attributes] change binding and sealing on class level.
  • Static.member, Final.member and Abstract.member designate a class, a sealed and an abstract member, while Public.member, Protected.member, and Private.member control the access (default set by $DefaultAccess ). The access attributes Private is partly checked statically for an unqualified access and an access through This. Other access checks are executed during evaluation. Polymorphism is controlled by means of Virtual.member and Real.member (default set by $DefaultInheritance ). Inheritance is emphasized by means of Override.member. Constant.method is a method that may not change fields of the object or class, but those qualified with Mutable. Export of fields can be controlled by means of Transient. An auomatic usage of access methods for fields is forced for fields with a Property modifier. A kind of operator overloading can be achieved by means of External. Any of the attributes returned by ClassAttributes[] may further specify the access to a method.
  • Environment[class] yields information on the class' context during its definition and Import[class] checks, if class was generated in another kernel session and imported into the current session.
This loads the package.
In[1]:=
Click for copyable input
The main entry point to this package is the function Class, which defines all classes. A class is (re)defined by assigning a set of definitions to the class. Usually, a delayed assignment (:=) is used for methods instead of a direct one (=) in order to prevent the evaluation of the definitions during the definition of the class itself. Using a direct assignment does not harm, if the symbols used in the member definitions have no definitions associated to them. The next example defines a class with one field and one method that is directly derived from Object, which is the base of all classes. The output is specially formatted and shows that a default constructor "base" was added automatically to the class, because there is no user-defined constructor. The last members take care of a symbol not found within the class and are printed in an abbreviated form.
In[2]:=
Click for copyable input
Out[2]//TableForm=
All class members only have a meaning in the context of an object created by the class. Directly using the method has no effect, because there is no definition made for meth itself.
In[3]:=
Click for copyable input
Out[3]=
Defining meth only affects the "global" function. The method of the class is not affected. We now define the "global" function. In order to access the class method, we create an object. New objects are automatically numbered and formatted specially in output. The different contexts of the function, class method and "global", cause a different behavior for both.
In[4]:=
Click for copyable input
In[5]:=
Click for copyable input
Out[5]=
Out[5]=
Out[5]=
Field values can be directly assigned.
In[6]:=
Click for copyable input
Out[6]=
In[7]:=
Click for copyable input
Out[7]//TableForm=
Objects automatically use the appropriate definitions (base defines "method[x_]:=x^2", while child sets "method[x_]:=x").
In[8]:=
Click for copyable input
Out[8]=
Out[8]=