Object Oriented Programing

Environment for an object-oriented-programming is supplied by:

Class:
The function to define a class.
context:
A class defines a context to define its all symbols for the variables and methods within the context. This automatically avoids conflicts of symbols between classes, Global, and System. When c = Class[ ... ] is done, a context c` is defined.
members:
The set of Members of a class is a union of class variables, instance variables, and class methods of the class.
operator @:
A special operator to access class member. In a notation f@g, g's context defaults the class of the class of f. f@g@h[x] is recognized as ((f@g)@h)[x] , thus the context of h defaults the class of f.
superclasses:
A class inherits all class variables, instance variables, class methods from its superclasses which are give by the first argument of Class. If a null list is given, Object` is set as the default superclass. Multiple inheritance is allowed.
class variable:
Class variables are given by the second argument of Class as a list of symbols. They are unique in the class. They can be initialized by declaring in a way such as {a=1, {b, c} = {2, 3}} like Module. A form like {a = b = c =1} is allowed.
instance variable:
Instance variables are given by the third argument of Class as a list of symbols. An instance have those symbols separately. They can be initialized by declaring in a way such as {a=1, {b, c} = {2, 3}} like as Module. A form like {a = b = c =1} is allowed. Also they are initialized at the creation of instance by rules as x = c[ a->1, b:>Print[d]], etc.
class methods:
Class methods are given by the fourth(last) argument of Class. They must be in the form of either one of
               f_[arg___] := g_;
               With[_, f_[arg___] := g_];
               With[_, f_[arg___] := g_; .. ];
               If[_,
                 ft_[argt___] := gt_; ..,
                 ft_[argf___] := gf_; ..,];
               h_[f_[arg___], b___] ^:= g_; .

where f is the symbol for the method to be defined. Set may be used instead of SetDelayed if necessary.

This:
A symbol This in the definition of the method, it is translated to the object (the instance or the class) which refers the member.
default reference:
In the definition of the class methods, whenever a member of the class is appeared, it is recognized as This@member. When a symbol of the member conflicts the symbol in System`, the system symbol should be wrapped by Literal.
reference of member of superclasses:
Members of the superclasses (denote cc) are refered by cc`member in the definition of the method.
copying an instance:
An instance c of a class can be copied to another symbol by c1 = c. After the copying, c1 and c refer the indentical instance. Destructing one of them by such as c1=. clears the instance and also all the assigned symbols.
Constructor:
When an instance is defined, by x = c[arg], a method x@Constructor[arg] is always invoked. In evaluation of instance definition under class scope, class member symbol appeared 1st slot of Rule or RuleDelayed argument is sent to Constructor of new class instance without evaluation. (In other term, class member symbol on 1st slot of Rule or RuleDelayed argument behaves like evaluating with implicit Literal[]) One can configure Constructor[] in the definition of the class. x = c[arg] returns the returned value of Constructor[arg]. The rules in the argument work in two ways: (1) A rule for an instance variable or a class variable sets the initial value of the variable, (2) Other rules are stored in an instance variable Options as a list.
Destructor:
An instance x is cleared by (x=.), which invokes x@Destructor[]. The default Destructor is Object`Destructor, but one can reconfigure it in the definition of the class.
Short:
When an instance x is returned as the result of expression for Out[], x@Short[] is invoked to show the result. The default Short is Object`Short, but one can reconfigure it in the definition of the class.
other methods:
Class[] gives the class of the instance. Parents[] gives the immediate superclasses. AllParents[] gives the all supreclasses. Members[] gives a list of class variables, class methods, and instance variables of the class. AllMembers[] gives a list of class variables, class methods, and instance variables of the class and its all parents.

Class sets up a class of objects.

Usage:   a = Class[
              list of superclasses,
              list of class-variables,
              list of instance-variables,
              class-methods];
Example: a = Class[
              {aa, bb},      (* aa and bb are superclasses *)
              {a1, a2},      (* class-variables *)
              {v1, v2},      (* instance-variables *)
              Constructor[arg__] := (Print[{arg}]; v1 = Plus[arg]);
              sum[] := v1 + v2 (* defining Constructor and method "sum"*)
            ];

        a1 = a[1, 2]          (* creating an instance of a *)
        a1@v1                 (* accessing an instance variable *)
        a1@v2 = 3             (* setting an instance variable *)
        a1@sum[]              (* calling a method "sum" *)
        a1=.                  (* delete an instance *)

トップ   差分 バックアップ リロード   一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2008-04-02 (水) 20:14:40