TrabalhosGratuitos.com - Trabalhos, Monografias, Artigos, Exames, Resumos de livros, Dissertações
Pesquisar

Um objeto contador

Artigo: Um objeto contador. Pesquise 860.000+ trabalhos acadêmicos

Por:   •  19/9/2013  •  Artigo  •  351 Palavras (2 Páginas)  •  256 Visualizações

Página 1 de 2

The last section defined one counter object. What do we do if we need more

than one counter? It would be nice to have a “factory” that can make as many

counters as we need. Such a factory is called a class. Here is one way to define

it:

declare

fun {NewCounter}

C Bump Read in

C={NewCell 0}

fun {Bump}

C:=@C+1

@C

end

fun {Read}

@C

end

counter(bump:Bump read:Read)

end

NewCounter is a function that creates a new cell and returns new Bump and Read

functions for it. Returning functions as results of functions is another form of

higher-order programming.

We group the Bump and Read functions together into one compound data

structure called a record. The record counter(bump:Bump read:Read) is characterized

by its label counter and by its two fields, called bump and read. Let

us create two counters:

declare

Ctr1={NewCounter}

Copyright

c 2001-3 by P. Van Roy and S. Haridi. All rights reserved.

1.15 Nondeterminism and time 21

C={NewCell 0} C:=2 C:=1

C={NewCell 0} C:=1 C:=2

time

final content of C is 1

final content of C is 2

First execution:

Second execution:

Figure 1.4: All possible executions of the first nondeterministic example

Ctr2={NewCounter}

Each counter has its own internal memory and its own Bump and Read functions.

We can access these functions by using the “.” (dot) operator. Ctr1.bump

accesses the Bump function of the first counter. Let us bump the first counter and

display its result:

{Browse {Ctr1.bump}}

Towards object-oriented programming

We have given an example of a simple class, NewCounter, that defines two operations,

Bump and Read. Operations defined inside classes are usually called

methods. The class can be used to make as many counter objects as we need.

All these objects share the

...

Baixar como (para membros premium)  txt (2.5 Kb)  
Continuar por mais 1 página »
Disponível apenas no TrabalhosGratuitos.com