Shared class members: Difference between revisions

From m204wiki
Jump to navigation Jump to search
(Automatically generated page update)
mNo edit summary
 
(2 intermediate revisions by the same user not shown)
Line 10: Line 10:
of the class, instantiating a new one as needed.
of the class, instantiating a new one as needed.
These members of a class are called '''shared''' members of the
These members of a class are called '''shared''' members of the
class in the <var class="product">[[Janus SOAP User Language Interface|Janus SOAP ULI]]</var>, and they are declared in <var>Private Shared</var> and <var>Public Shared</var> blocks.
class in <var class="product">SOUL</var>, and they are declared in <var>Private Shared</var> and <var>Public Shared</var> blocks.
   
   
Most other object-oriented languages have the same concept as shared class members,
Most other object-oriented languages have the same concept as shared class members,
Line 20: Line 20:
<li>They are usually
<li>They are usually
allocated statically in memory (as is the case with shared variables
allocated statically in memory (as is the case with shared variables
in the <var class="product">Janus SOAP ULI</var>).
in <var class="product">SOUL</var>).
</ul>
</ul>
   
   
The following illustrates the use of a shared variable called <code>MaxScore</code>
The following illustrates the use of a shared variable called <code>MaxScore</code>
that holds the highest value for another class variable called <code>Score</code>:
that holds the highest value for another class variable called <code>Score</code>:
<p class="code"> class dog
<p class="code">class dog
    public
  public
      property score is float
      property score is float
    end public
  end public
    private
  private
      variable varScore is float
      variable varScore is float
    end private
  end private
    public shared
  public shared
      variable maxScore is float
      variable maxScore is float
    end public shared
  end public shared
   
   
    property score is float
  property score is float
      get
      get
          return %varScore
        return %varScore
      end get
      end get
      set
      set
          %varScore = %score
        %varScore = %score
          if %score gt %maxScore then
        if %score gt %maxScore then
            %maxScore = %score
            %maxScore = %score
          end if
        end if
      end set
      end set
    end property
  end property
  end class
  end class
</p>
</p>
As with instance-specific variables, the shared variable <code>MaxScore</code> can
As with instance-specific variables, the shared variable <code>MaxScore</code> can
be accessed via an object variable of the class:
be accessed via an object variable of the class:
<p class="code"> %competitor is object dog
<p class="code">%competitor is object dog
  ...
...
print %competitor:maxScore
print %competitor:maxScore
</p>
</p>
Being a shared variable, however, the specific object used to access
Being a shared variable, however, the specific object used to access
Line 60: Line 60:
Shared variables can also be accessed by using the class name enclosed
Shared variables can also be accessed by using the class name enclosed
in parentheses as a variable name rather than an actual object variable:
in parentheses as a variable name rather than an actual object variable:
<p class="code"> print %(dog):maxScore
<p class="code">print %(dog):maxScore
</p>
</p>
   
   
Line 66: Line 66:
might be useful to allocate a "dummy" object variable with the
might be useful to allocate a "dummy" object variable with the
same name as the class and to use that to reference shared variables:
same name as the class and to use that to reference shared variables:
<p class="code"> %dog  is object dog
<p class="code">%dog  is object dog
  ...
...
print %dog:maxScore
print %dog:maxScore
</p>
</p>
   
   
Line 88: Line 88:
For example, in the following class definition, shared method <code>TotalPrice</code>
For example, in the following class definition, shared method <code>TotalPrice</code>
calculates the total price of all items on a chain of items:
calculates the total price of all items on a chain of items:
<p class="code"> class purchase
<p class="code">class purchase
    private
  private
      variable itemNumber is fixed
      variable itemNumber is fixed
      variable itemName  is string len 32
      variable itemName  is string len 32
      variable next      is object purchase
      variable next      is object purchase
      variable quantity  is fixed
      variable quantity  is fixed
      variable price      is float
      variable price      is float
        ...
      ...
    end private
  end private
    private shared
  private shared
      variable first      is object purchase
      variable first      is object purchase
    end private shared
  end private shared
    public shared
  public shared
      function totalPrice is float
      function totalPrice is float
    end public shared
  end public shared
   
   
    function totalPrice is float
  function totalPrice is float
      %current is object purchase
      %current is object purchase
      %total  is float
      %total  is float
      %current = %first
      %current = %first
      repeat while %current ne null
      repeat while %current ne null
          %total = %total + %current:price * %current:quantity
        %total = %total + %current:price * %current:quantity
          %current = %current:next
        %current = %current:next
      end repeat
      end repeat
      return %total
      return %total
    end function
  end function
   
   
end class
end class
</p>
</p>
'''Note:'''
<blockquote class="note">'''Note:'''
In a shared method, the variable <var>%this</var> is not defined because there is
In a shared method, the variable <var>%this</var> is not defined because there is
no current object instance, and non-shared variables and methods cannot
no current object instance, and non-shared variables and methods cannot
be accessed without an object variable.
be accessed without an object variable.
Shared variables (in this example, <code>%first</code>), however,
Shared variables (in this example, <code>%first</code>), however,
can be accessed without any extra qualification.
can be accessed without any extra qualification.  
<p>
The <var>This</var> keyword can be used to indicate the current class for
The <var>This</var> keyword can be used to indicate the current class for
shared member references inside the class.
shared member references inside the class.
For example, in the following, the word <code>This</code> inside parentheses
For example, in the following, the word <code>This</code> inside parentheses
means the current class, that is, class Foo:
means the current class, that is, class <code>Foo</code>: </p>
<p class="code"> class foo
<p class="code">class foo
    public
  public
      subroutine increment
      subroutine increment
    end public
  end public
    public shared
  public shared
      variable counter is float
      variable counter is float
    end public
  end public
    subroutine increment
  subroutine increment
      %(this):counter = %(this):counter + 1
      %(this):counter = %(this):counter + 1
    end subroutine
  end subroutine
end class
end class
</p>
</p>
While, in his example, <code>%(this):counter</code> could have been written as
While, in his example, <code>%(this):counter</code> could have been written as
simply <code>%counter</code>, you might want to use the former syntax to highlight
simply <code>%counter</code>, you might want to use the former syntax to highlight
the fact that <code>Counter</code> is a shared member of the current class.
the fact that <code>Counter</code> is a shared member of the current class.
</blockquote>
   
   
Unlike non-shared variables, shared variables can be <var>Static</var> or
Unlike non-shared variables, shared variables can be <var>Static</var> or

Latest revision as of 17:59, 22 October 2013


As the term "object-oriented programming" suggests, most code tends to be associated with objects, that is, instances of a class. However, it is also quite common to have class variables or methods that are independent of a specific instance of a class. For example, a class variable may contain the maximum valid value for some other class variable. Or a function may return a "work" instance of the class, instantiating a new one as needed. These members of a class are called shared members of the class in SOUL, and they are declared in Private Shared and Public Shared blocks.

Most other object-oriented languages have the same concept as shared class members, though in some of these other languages these members are called static members because:

  • These variables are shared among all instances of a class and so have only one instance in the whole program.
  • They are usually allocated statically in memory (as is the case with shared variables in SOUL).

The following illustrates the use of a shared variable called MaxScore that holds the highest value for another class variable called Score:

class dog public property score is float end public private variable varScore is float end private public shared variable maxScore is float end public shared property score is float get return %varScore end get set %varScore = %score if %score gt %maxScore then %maxScore = %score end if end set end property end class

As with instance-specific variables, the shared variable MaxScore can be accessed via an object variable of the class:

%competitor is object dog ... print %competitor:maxScore

Being a shared variable, however, the specific object used to access the shared variable is irrelevant, so it doesn't even matter if the object used to access the shared variable is null. Shared variables can also be accessed by using the class name enclosed in parentheses as a variable name rather than an actual object variable:

print %(dog):maxScore

If access to shared variables via the class name is a common practice, it might be useful to allocate a "dummy" object variable with the same name as the class and to use that to reference shared variables:

%dog is object dog ... print %dog:maxScore

This is no different from accessing the shared variable via the class name in parentheses but might be viewed as aesthetically more appealing. Making this approach a standard, however, entails the requirement that when a new class is introduced, not only does the class name need to be unique among class names but also among local and common variables used anywhere the class might be used.

The term "shared method" is something of a misnomer (as is the term "static method" used in other object-oriented languages) — the code of all methods is shared among all instances of a class. What distinguishes a shared method is that a shared method does not operate on an instance of the class but instead operates independent of any instances.

For example, in the following class definition, shared method TotalPrice calculates the total price of all items on a chain of items:

class purchase private variable itemNumber is fixed variable itemName is string len 32 variable next is object purchase variable quantity is fixed variable price is float ... end private private shared variable first is object purchase end private shared public shared function totalPrice is float end public shared function totalPrice is float %current is object purchase %total is float %current = %first repeat while %current ne null %total = %total + %current:price * %current:quantity %current = %current:next end repeat return %total end function end class

Note:

In a shared method, the variable %this is not defined because there is no current object instance, and non-shared variables and methods cannot be accessed without an object variable. Shared variables (in this example, %first), however, can be accessed without any extra qualification.

The This keyword can be used to indicate the current class for shared member references inside the class. For example, in the following, the word This inside parentheses means the current class, that is, class Foo:

class foo public subroutine increment end public public shared variable counter is float end public subroutine increment  %(this):counter = %(this):counter + 1 end subroutine end class

While, in his example, %(this):counter could have been written as simply %counter, you might want to use the former syntax to highlight the fact that Counter is a shared member of the current class.

Unlike non-shared variables, shared variables can be Static or Global. Shared Static variables are a useful place to keep class-wide constants.

While both of the examples in this section demonstrated public shared members, shared members can also be private. Shared private variables must, of course, be declared in a Private Shared block.